-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquest20.rs
217 lines (176 loc) · 6.06 KB
/
quest20.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
use crate::util::grid::*;
use crate::util::point::*;
use std::collections::VecDeque;
const FLY: [Point; 4] = [DOWN, LEFT, RIGHT, UP];
const TURN: [[usize; 3]; 4] = [[0, 1, 2], [0, 1, 3], [0, 2, 3], [1, 2, 3]];
pub fn part1(notes: &str) -> i32 {
let grid = Grid::parse(notes);
let start = grid.find(b'S').unwrap();
let mut todo = VecDeque::from([(start, 0, 0, 0)]);
let mut seen = minimum_from(&grid);
let mut result = 0;
while let Some((point, direction, altitude, time)) = todo.pop_front() {
if time == 100 {
result = result.max(altitude);
continue;
}
for turn in TURN[direction] {
let next = point + FLY[turn];
if grid.contains(next) && grid[next] != b'#' {
let altitude = match grid[next] {
b'+' => altitude + 1,
b'-' => altitude - 2,
_ => altitude - 1,
};
if seen[next][turn] < altitude {
todo.push_back((next, turn, altitude, time + 1));
seen[next][turn] = altitude;
}
}
}
}
1000 + result
}
pub fn part2(notes: &str) -> i32 {
let mut grid = Grid::parse(notes);
let mut waypoints = [b'S', b'A', b'B', b'C'].map(|p| grid.find(p).unwrap());
let mut total = 0;
for _ in 0..4 {
grid[waypoints[0]] = b'#';
grid[waypoints[1]] = b'.';
grid[waypoints[2]] = b'#';
grid[waypoints[3]] = b'#';
total += segment(&grid, waypoints[0], waypoints[1]);
waypoints.rotate_left(1);
}
total
}
#[expect(clippy::needless_range_loop)]
pub fn part3(notes: &str) -> i32 {
let grid = Grid::parse(notes);
let start = grid.find(b'S').unwrap();
let mut extended = grid.clone();
extended.height += 1;
extended.bytes.extend_from_slice(&grid.bytes[0..grid.width as usize]);
let start = start.x as usize;
let size = grid.width as usize;
let left = 1;
let right = size - 1;
// Caculate all possible combinations of altitude drops.
let mut drop = vec![vec![]; size];
for from in left..right {
drop[from] = complete(&extended, from);
}
let mut current = vec![i32::MIN; size];
current[start] = 384400;
for block in 0.. {
let mut next = vec![i32::MIN; size];
for from in left..right {
for to in left..right {
let candidate = current[from].saturating_add(drop[from][to]);
next[to] = next[to].max(candidate);
}
}
if next.iter().all(|&a| a <= 0) {
let complete = block * grid.height;
let mut partial = 0;
for x in left..right {
if current[x] > 0 {
partial = partial.max(remainder(&grid, x, current[x]));
}
}
return complete + partial;
}
current = next;
}
unreachable!()
}
fn segment(grid: &Grid<u8>, start: Point, end: Point) -> i32 {
let mut todo = VecDeque::from([(start, 0, 0, 0)]);
let mut seen = minimum_from(grid);
while let Some((point, direction, altitude, time)) = todo.pop_front() {
if point == end {
if altitude >= 0 {
return time;
}
continue;
}
for turn in TURN[direction] {
let next = point + FLY[turn];
if grid.contains(next) && grid[next] != b'#' {
let altitude = match grid[next] {
b'+' => altitude + 1,
b'-' => altitude - 2,
_ => altitude - 1,
};
if seen[next][turn] < altitude {
todo.push_back((next, turn, altitude, time + 1));
seen[next][turn] = altitude;
}
}
}
}
unreachable!()
}
fn complete(grid: &Grid<u8>, start: usize) -> Vec<i32> {
let start = Point::new(start as i32, 0);
let mut todo = VecDeque::from([(start, 0, 0, 0)]);
let mut seen = minimum_from(grid);
let mut result = vec![i32::MIN; grid.width as usize];
while let Some((point, direction, altitude, time)) = todo.pop_front() {
if point.y == grid.height - 1 {
let index = point.x as usize;
result[index] = result[index].max(altitude);
continue;
}
for turn in TURN[direction] {
let next = point + FLY[turn];
if grid.contains(next) && grid[next] != b'#' {
let altitude = match grid[next] {
b'+' => altitude + 1,
b'-' => altitude - 2,
_ => altitude - 1,
};
if seen[next][turn] < altitude {
todo.push_back((next, turn, altitude, time + 1));
seen[next][turn] = altitude;
}
}
}
}
result
}
fn remainder(grid: &Grid<u8>, start: usize, height: i32) -> i32 {
let start = Point::new(start as i32, 0);
let mut todo = VecDeque::from([(start, 0, height, 0)]);
let mut seen = minimum_from(grid);
let mut result = 0;
while let Some((point, direction, altitude, time)) = todo.pop_front() {
if altitude <= 0 {
result = result.max(point.y);
continue;
}
for turn in TURN[direction] {
let next = point + FLY[turn];
if grid.contains(next) && grid[next] != b'#' {
let altitude = match grid[next] {
b'+' => altitude + 1,
b'-' => altitude - 2,
_ => altitude - 1,
};
if seen[next][turn] < altitude {
todo.push_back((next, turn, altitude, time + 1));
seen[next][turn] = altitude;
}
}
}
}
result
}
fn minimum_from(grid: &Grid<u8>) -> Grid<[i32; 4]> {
Grid {
width: grid.width,
height: grid.height,
bytes: (0..grid.width * grid.height).map(|_| [i32::MIN; 4]).collect(),
}
}