-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path24.rs
192 lines (169 loc) · 4.97 KB
/
24.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#![feature(test)]
use std::{cmp::Ordering, collections::BinaryHeap};
use itertools::Itertools;
use rustc_hash::FxHashSet;
use smallvec::{smallvec, SmallVec};
#[derive(Debug)]
struct Input {
width: usize,
height: usize,
blizzards: Vec<Vec<Option<Direction>>>,
}
#[derive(Debug, Clone, Copy)]
enum Direction {
North,
East,
South,
West,
}
fn setup(input: &str) -> Input {
let blizzards = input
.lines()
.skip(1)
.take_while(|l| l.as_bytes()[1] != b'#')
.map(|line| {
line.bytes()
.skip(1)
.take_while(|&c| c != b'#')
.map(|c| match c {
b'.' => None,
b'^' => Some(Direction::North),
b'>' => Some(Direction::East),
b'v' => Some(Direction::South),
b'<' => Some(Direction::West),
_ => panic!(),
})
.collect_vec()
})
.collect_vec();
Input {
width: blizzards[0].len(),
height: blizzards.len(),
blizzards,
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
struct State {
time: usize,
position: usize,
cnt: usize,
dist: usize,
}
impl PartialOrd for State {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for State {
fn cmp(&self, other: &Self) -> Ordering {
other.dist.cmp(&self.dist)
}
}
impl State {
fn init(input: &Input) -> Self {
Self {
time: 0,
position: input.width * input.height,
cnt: 0,
dist: usize::MAX,
}
}
fn next(self, input: &Input, target: usize) -> impl Iterator<Item = Self> + '_ {
let &Input {
width: w,
height: h,
..
} = input;
let mut out: SmallVec<[_; 7]> = smallvec![self.position];
match self.position {
p if p == w * h => out.push(0),
p if p == w * h + 1 => out.push(w * h - 1),
p => {
if p == 0 {
out.push(w * h);
}
if p == w * h - 1 {
out.push(w * h + 1);
}
if p >= w {
out.push(p - w);
}
if p + w < w * h {
out.push(p + w);
}
if p % w > 0 {
out.push(p - 1);
}
if p % w < w - 1 {
out.push(p + 1);
}
}
}
out.into_iter()
.filter(move |&p| {
let (i, j) = (p / w, p % w);
let t = self.time + 1;
p >= w * h
|| !matches!(
input.blizzards[i][(j + w - t % w) % w],
Some(Direction::East)
) && !matches!(input.blizzards[i][(j + t) % w], Some(Direction::West))
&& !matches!(
input.blizzards[(i + h - t % h) % h][j],
Some(Direction::South)
)
&& !matches!(input.blizzards[(i + t) % h][j], Some(Direction::North))
})
.map(move |p| {
let time = self.time + 1;
let cnt = self.cnt
+ (self.cnt % 2) * (p == w * h) as usize
+ ((self.cnt + 1) % 2) * (p == w * h + 1) as usize;
let start_to_goal = w + h;
let (start, goal) = match p {
p if p == w * h => (0, start_to_goal),
p if p == w * h + 1 => (start_to_goal, 0),
p => {
let (i, j) = (p / w, p % w);
(i + j + 1, start_to_goal - i - j - 2)
}
};
Self {
time,
cnt,
position: p,
dist: {
if cnt != target {
time + start * (cnt % 2)
+ goal * ((cnt + 1) % 2)
+ (target - cnt - 1) * start_to_goal
} else {
0
}
},
}
})
}
}
fn solve(input: &Input, target: usize) -> usize {
let mut queue = BinaryHeap::from([State::init(input)]);
let mut seen = FxHashSet::default();
while let Some(state) = queue.pop() {
if state.cnt == target {
return state.time;
}
for next in state.next(input, target) {
if seen.insert(next) {
queue.push(next);
}
}
}
panic!()
}
fn part1(input: &Input) -> usize {
solve(input, 1)
}
fn part2(input: &Input) -> usize {
solve(input, 3)
}
aoc::main!(2022, 24, ex: 1);