-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
381 lines (322 loc) · 9.54 KB
/
main.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
use std::{
fmt::Debug,
io::{Error, Read},
ops::{Index, IndexMut},
};
#[derive(Clone, Copy, PartialEq, Eq)]
enum Tile {
Start,
Plot,
Rock,
O,
}
impl Debug for Tile {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Tile::Start => write!(f, "S"),
Tile::Plot => write!(f, "."),
Tile::Rock => write!(f, "#"),
Tile::O => write!(f, "O"),
}
}
}
impl TryFrom<char> for Tile {
type Error = String;
fn try_from(value: char) -> Result<Self, Self::Error> {
match value {
'S' => Ok(Self::Start),
'.' => Ok(Self::Plot),
'#' => Ok(Self::Rock),
'O' => Ok(Self::O),
_ => Err(format!("unknown tile: {}", value)),
}
}
}
#[derive(Debug, Clone)]
struct Grid {
grid: Vec<Vec<Tile>>,
rows: usize,
columns: usize,
}
impl Grid {
fn lines(&self) -> GridIterator {
GridIterator {
grid: self,
current_row: 0,
}
}
fn get(&self, (x, y): (usize, usize)) -> Option<&Tile> {
if x < self.columns && y < self.rows {
Some(&self.grid[y][x])
} else {
None
}
}
fn get_mut(&mut self, (x, y): (usize, usize)) -> Option<&mut Tile> {
if x < self.columns && y < self.rows {
Some(&mut self.grid[y][x])
} else {
None
}
}
// naming things is hard
// (see part 2 comments for what this does)
fn make_big(&mut self) {
// find s
let mut s_pos = (0, 0);
for (j, line) in self.grid.iter().enumerate() {
for (i, tile) in line.iter().enumerate() {
if *tile == Tile::Start {
s_pos = (i, j);
break;
}
}
}
// remove s
self[s_pos] = Tile::Plot;
// extend horizontally
for line in self.grid.iter_mut() {
let clone = line.clone();
line.extend(clone.clone());
line.extend(clone.clone());
line.extend(clone.clone());
line.extend(clone);
}
// extend vertically
let clone = self.grid.clone();
self.grid.extend(clone.clone());
self.grid.extend(clone.clone());
self.grid.extend(clone.clone());
self.grid.extend(clone);
// add s back in
self.grid[self.rows * 2 + s_pos.1][self.columns * 2 + s_pos.0] = Tile::Start;
self.columns *= 5;
self.rows *= 5;
}
}
impl TryFrom<&str> for Grid {
type Error = String;
fn try_from(input: &str) -> Result<Self, Self::Error> {
let trimmed = input.trim();
let rows = trimmed.lines().count();
let first_line = trimmed.lines().next().ok_or("input is empty")?;
let columns = first_line.len();
let mut grid = Vec::with_capacity(rows);
for line in trimmed.lines() {
if line.len() != columns {
return Err("not a grid".into());
}
let tiles = line
.chars()
.map(Tile::try_from)
.collect::<Result<Vec<Tile>, _>>()?;
grid.push(tiles);
}
Ok(Grid {
grid,
rows,
columns,
})
}
}
struct GridIterator<'a> {
grid: &'a Grid,
current_row: usize,
}
impl<'a> Iterator for GridIterator<'a> {
type Item = &'a [Tile];
fn next(&mut self) -> Option<Self::Item> {
if self.current_row < self.grid.rows {
let r = Some(&self.grid[self.current_row]);
self.current_row += 1;
r
} else {
None
}
}
}
impl Index<(usize, usize)> for Grid {
type Output = Tile;
fn index(&self, index: (usize, usize)) -> &Self::Output {
self.get(index).unwrap()
}
}
impl IndexMut<(usize, usize)> for Grid {
fn index_mut(&mut self, index: (usize, usize)) -> &mut Self::Output {
self.get_mut(index).unwrap()
}
}
impl Index<usize> for Grid {
type Output = [Tile];
fn index(&self, index: usize) -> &Self::Output {
&self.grid[index]
}
}
impl IndexMut<usize> for Grid {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
&mut self.grid[index]
}
}
fn take_step(grid: &mut Grid) {
let mut starts = Vec::new();
grid.lines().enumerate().for_each(|(row, tiles)| {
starts.extend(
tiles
.iter()
.enumerate()
.filter_map(|(column, tile)| match tile {
Tile::Start | Tile::O => Some((column, row)),
Tile::Plot | Tile::Rock => None,
}),
);
});
// first mark all starts as plots
for &start in &starts {
grid[start] = Tile::Plot;
}
// then mark all the possible steps
for &start in &starts {
// west
if start.0 > 0 {
if let Some(tile) = grid.get_mut((start.0 - 1, start.1)) {
match tile {
Tile::Plot => *tile = Tile::O,
Tile::Start => unreachable!(),
_ => {}
}
}
}
// east
if let Some(tile) = grid.get_mut((start.0 + 1, start.1)) {
match tile {
Tile::Plot => *tile = Tile::O,
Tile::Start => unreachable!(),
_ => {}
}
}
// north
if start.1 > 0 {
if let Some(tile) = grid.get_mut((start.0, start.1 - 1)) {
match tile {
Tile::Plot => *tile = Tile::O,
Tile::Start => unreachable!(),
_ => {}
}
}
}
// south
if let Some(tile) = grid.get_mut((start.0, start.1 + 1)) {
match tile {
Tile::Plot => *tile = Tile::O,
Tile::Start => unreachable!(),
_ => {}
}
}
}
}
fn count_os(grid: &Grid) -> usize {
grid.lines()
.map(|tiles| {
tiles
.iter()
.filter_map(|tile| match tile {
Tile::Start | Tile::O => Some(1),
_ => None,
})
.sum::<usize>()
})
.sum()
}
fn part1(input: &str, steps: usize) -> usize {
let mut grid = Grid::try_from(input).unwrap();
for _ in 0..steps {
take_step(&mut grid);
}
count_os(&grid)
}
fn aitken_neville(v0: usize, v1: usize, v2: usize, x: usize) -> usize {
let mut p = [v0, v1, v2];
for i in 1..3 {
for j in 0..3 - i {
p[j] = p[j] + (x - j) / ((i + j) - j) * (p[j + 1] - p[j]);
}
}
p[0]
}
// I honestly still don't understand this one.
// Mostly solved with the help of reddit comments.
// Supposedly by calculating the reached tiles for 65, 65 + 131 and 65 + 131 * 2 steps,
// one can use the resulting values to extrapolate.
//
// It has something to do with how the input is well formed again.
// For one, the starting point has no obstacles to all the edges,
// then the edges themselves also have no rocks,
// and lastly there is this big diamond of plot in the input that goes from edge to edge (most
// easily seen with those code-minimaps from vscode or sublime).
// Also, the starting point is right in the middle of the grid,
// the grid is 131 wide and high (making it a square), and that's where
// the 65 (= floor(131/2)) and 131 constants come from.
// Lastly, the number of steps in the puzzle question is
// 26501365, while 26501365 mod 131 = 65.
//
// Because of that, we want a function of the form f(x) = reached tiles in 65 + 131 * x steps.
// And because of the observations above, that function happens to be quadratic (no idea why).
// So, all we have to do is get the first 3 values (i.e. f(0), f(1) and f(2)), then we can uniquely
// calculate the actual quadratic function, and then just evaluate f((26501365 - 65) / 131)
// or more specifically f(202300).
//
// In this case, because we need only the value of a single argument,
// the Aitken Neville scheme fit well.
// The code of Aitken Neville above is copied from lecture slides of mine.
fn part2(input: &str) -> usize {
let mut grid = Grid::try_from(input).unwrap();
// To find the values of the first 3 xs, we first need to make the grid sufficiently large.
// `make_big` just extends the grid by 5 in each direction.
// 5 is just a random value that turned out to be enough.
grid.make_big();
for _ in 0..65 {
take_step(&mut grid);
}
let v0 = count_os(&grid);
// println!("0: {}", count_os(&grid));
for _ in 0..131 {
take_step(&mut grid);
}
let v1 = count_os(&grid);
// println!("1: {}", count_os(&grid));
for _ in 0..131 {
take_step(&mut grid);
}
let v2 = count_os(&grid);
// println!("2: {}", count_os(&grid));
aitken_neville(v0, v1, v2, (26501365 - 65) / 131)
}
fn main() -> Result<(), Error> {
let mut input = String::new();
let _ = std::io::stdin().read_to_string(&mut input)?;
println!("Part 1: {}", part1(&input, 64));
println!("Part 2: {}", part2(&input));
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
const EXAMPLE: &str = "...........
.....###.#.
.###.##..#.
..#.#...#..
....#.#....
.##..S####.
.##..#...#.
.......##..
.##.#.####.
.##..##.##.
...........
";
#[test]
fn test_part1() {
let expected = 16;
let actual = part1(EXAMPLE, 6);
assert_eq!(expected, actual);
}
}