-
Notifications
You must be signed in to change notification settings - Fork 15
/
day06.rs
218 lines (186 loc) · 6.25 KB
/
day06.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
//! # Guard Gallivant
//!
//! Part two is sped up by pre-computing the next obstacle in each direction from any point in
//! the grid. If there is nothing left in the way then coordinates outside the grid are used.
//! One dimensional example:
//!
//! ```none
//! .#...
//! Left: (-1, 2, 2, 2, 2)
//! Right: (1, 1, 5, 5, 5)
//! ```
//!
//! This allows us to "shortcut" to each obstacle when looking for cycles. The remaining tricky
//! part is including the extra obstacle which is different for each point on the guard's path.
//!
//! The search can be parallelized across multiple threads as each position is independent.
use crate::util::grid::*;
use crate::util::hash::*;
use crate::util::point::*;
use crate::util::thread::*;
use std::sync::atomic::{AtomicUsize, Ordering};
pub fn parse(input: &str) -> Grid<u8> {
Grid::parse(input)
}
/// Count distinct positions in the guard's path, which will eventually leave the grid.
pub fn part1(grid: &Grid<u8>) -> usize {
let mut grid = grid.clone();
let mut position = grid.find(b'^').unwrap();
let mut direction = UP;
let mut result = 1;
while grid.contains(position + direction) {
if grid[position + direction] == b'#' {
direction = direction.clockwise();
continue;
}
let next = position + direction;
// Avoid double counting when the path crosses itself.
if grid[next] == b'.' {
result += 1;
grid[next] = b'^';
}
position = next;
}
result
}
/// Follow the guard's path, checking every step for a potential cycle.
pub fn part2(grid: &Grid<u8>) -> usize {
let mut grid = grid.clone();
let mut position = grid.find(b'^').unwrap();
let mut direction = UP;
let mut path = Vec::with_capacity(5_000);
while grid.contains(position + direction) {
if grid[position + direction] == b'#' {
direction = direction.clockwise();
}
let next = position + direction;
// Avoid double counting when the path crosses itself.
if grid[next] == b'.' {
path.push((position, direction));
grid[next] = b'^';
}
position = next;
}
// Use as many cores as possible to parallelize the remaining search.
let shortcut = Shortcut::from(&grid);
let total = AtomicUsize::new(0);
spawn_parallel_iterator(&path, |iter| worker(&shortcut, &total, iter));
total.into_inner()
}
fn worker(shortcut: &Shortcut, total: &AtomicUsize, iter: ParIter<'_, (Point, Point)>) {
let mut seen = FastSet::new();
let result = iter
.filter(|(position, direction)| {
seen.clear();
is_cycle(shortcut, &mut seen, *position, *direction)
})
.count();
total.fetch_add(result, Ordering::Relaxed);
}
fn is_cycle(
shortcut: &Shortcut,
seen: &mut FastSet<(Point, Point)>,
mut position: Point,
mut direction: Point,
) -> bool {
let obstacle = position + direction;
while shortcut.up.contains(position) {
// Reaching the same position in the same direction is a cycle.
if !seen.insert((position, direction)) {
return true;
}
// The tricky part is checking for the new time travelling instigated obstacle.
position = match direction {
UP => {
let next = shortcut.up[position];
if position.x == obstacle.x && position.y > obstacle.y && obstacle.y >= next.y {
obstacle - UP
} else {
next
}
}
DOWN => {
let next = shortcut.down[position];
if position.x == obstacle.x && position.y < obstacle.y && obstacle.y <= next.y {
obstacle - DOWN
} else {
next
}
}
LEFT => {
let next = shortcut.left[position];
if position.y == obstacle.y && position.x > obstacle.x && obstacle.x >= next.x {
obstacle - LEFT
} else {
next
}
}
RIGHT => {
let next = shortcut.right[position];
if position.y == obstacle.y && position.x < obstacle.x && obstacle.x <= next.x {
obstacle - RIGHT
} else {
next
}
}
_ => unreachable!(),
};
direction = direction.clockwise();
}
false
}
struct Shortcut {
up: Grid<Point>,
down: Grid<Point>,
left: Grid<Point>,
right: Grid<Point>,
}
impl Shortcut {
fn from(grid: &Grid<u8>) -> Self {
let mut up = grid.same_size_with(ORIGIN);
let mut down = grid.same_size_with(ORIGIN);
let mut left = grid.same_size_with(ORIGIN);
let mut right = grid.same_size_with(ORIGIN);
for x in 0..grid.width {
let mut last = Point::new(x, -1);
for y in 0..grid.height {
let point = Point::new(x, y);
if grid[point] == b'#' {
last = Point::new(x, y + 1);
}
up[point] = last;
}
}
for x in 0..grid.width {
let mut last = Point::new(x, grid.height);
for y in (0..grid.height).rev() {
let point = Point::new(x, y);
if grid[point] == b'#' {
last = Point::new(x, y - 1);
}
down[point] = last;
}
}
for y in 0..grid.height {
let mut last = Point::new(-1, y);
for x in 0..grid.width {
let point = Point::new(x, y);
if grid[point] == b'#' {
last = Point::new(x + 1, y);
}
left[point] = last;
}
}
for y in 0..grid.height {
let mut last = Point::new(grid.width, y);
for x in (0..grid.width).rev() {
let point = Point::new(x, y);
if grid[point] == b'#' {
last = Point::new(x - 1, y);
}
right[point] = last;
}
}
Shortcut { up, down, left, right }
}
}