-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06.rs
126 lines (99 loc) · 3.02 KB
/
06.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
use std::collections::HashSet;
use advent_of_code::{Compass, Grid};
advent_of_code::solution!(6);
fn get_seen_positions(start: usize, grid: &Grid<char>) -> HashSet<usize> {
let mut seen = HashSet::new();
let mut d = Compass::N;
let mut p = start;
seen.insert(p);
while let Some(next) = grid.step_from_index(p, d) {
if grid.data[next] == '#' {
d = d.turn_right();
} else {
p = next;
seen.insert(p);
}
}
seen
}
pub fn part_one(input: &str) -> Option<usize> {
let grid: Grid<char> = Grid::parse_lines(input);
let start = grid
.data
.iter()
.enumerate()
.find_map(|(i, c)| (*c == '^').then_some(i))
.unwrap();
Some(get_seen_positions(start, &grid).len())
}
fn has_cycle(
start: usize,
grid: &Grid<char>,
working_space: &mut [[u16; 4]],
generation: u16,
) -> bool {
let mut d = Compass::N;
let mut p = start;
working_space[p][d as u8 as usize] = generation;
while let Some(next) = grid.step_from_index(p, d) {
if grid.data[next] == '#' {
d = d.turn_right();
} else {
p = next;
let cell = &mut working_space[p][d as u8 as usize];
if *cell == generation {
return true;
}
*cell = generation;
}
}
false
}
pub fn part_two(input: &str) -> Option<usize> {
let mut grid: Grid<char> = Grid::parse_lines(input);
let start = grid
.data
.iter()
.enumerate()
.find_map(|(i, c)| (*c == '^').then_some(i))
.unwrap();
let possible_positions = get_seen_positions(start, &grid);
// Originally I detected cycles with a hashmap, but that was very slow.
// This method uses a fixed (but large) amount of memory for all checks of
// cycles. We duplicate the shape of the grid with a slot for each direction
// the guard could be facing there.
//
// For each possible position of the new obstacle, we set a new "generation"
// and use that as the marker for position-direction pairs, if a traversal
// of the new grid encounters a slot with the current generation, it
// indicates a repeated vector and a cycle
let mut cycle_detector = vec![[0; 4]; grid.data.len()];
let result = possible_positions
.into_iter()
.enumerate()
.filter(|(i, ob)| {
grid.data[*ob] = '#';
let result =
has_cycle(start, &grid, &mut cycle_detector, *i as u16 + 1);
grid.data[*ob] = '.';
result
})
.count();
Some(result)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let result =
part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(41));
}
#[test]
fn test_part_two() {
let result =
part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(6));
}
}