-
Notifications
You must be signed in to change notification settings - Fork 0
/
day6.rs
178 lines (149 loc) · 4.76 KB
/
day6.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
//! [Day 6: Guard Gallivant](https://adventofcode.com/2024/day/6)
use aoc::{grid, grid::Direction, grid::Grid};
use std::collections::HashSet;
struct Puzzle {
grid: Grid<char>,
start: (usize, usize),
}
impl Puzzle {
fn new() -> Self {
Self {
grid: grid![],
start: (0, 0),
}
}
/// Get the puzzle input.
fn configure(&mut self, path: &str) {
let data = std::fs::read_to_string(path).unwrap();
self.grid = aoc::grid::Grid::<char>::parse(&data);
for (xy, p) in self.grid.iter() {
if p == &'^' {
self.start = xy;
break;
}
}
}
fn move_guard(
&self,
x: &mut usize,
y: &mut usize,
direction: &mut grid::Direction,
obstruction: (usize, usize),
) -> bool {
match direction {
Direction::East => {
if *x == 0 {
return true;
} else if self.grid[(*x - 1, *y)] == '#' || (*x - 1, *y) == obstruction {
*direction = Direction::North;
} else {
*x -= 1;
}
}
Direction::West => {
if *x == self.grid.size().0 - 1 {
return true;
} else if self.grid[(*x + 1, *y)] == '#' || (*x + 1, *y) == obstruction {
*direction = Direction::South;
} else {
*x += 1;
}
}
Direction::North => {
if *y == 0 {
return true;
} else if self.grid[(*x, *y - 1)] == '#' || (*x, *y - 1) == obstruction {
*direction = Direction::West;
} else {
*y -= 1;
}
}
Direction::South => {
if *y == self.grid.size().1 - 1 {
return true;
} else if self.grid[(*x, *y + 1)] == '#' || (*x, *y + 1) == obstruction {
*direction = Direction::East;
} else {
*y += 1;
}
}
};
false
}
/// Solve part one.
fn part1(&self) -> usize {
let (mut x, mut y) = self.start;
let mut direction = grid::Direction::North;
let mut leave = false;
let mut seen = HashSet::new();
let obstruction = (usize::MAX, usize::MAX);
while !leave {
seen.insert((x, y));
leave = self.move_guard(&mut x, &mut y, &mut direction, obstruction);
}
seen.len()
}
/// Solve part two.
fn part2(&self) -> u32 {
// repeat part 1 to eliminate positions that are never visited
let (mut x, mut y) = self.start;
let mut direction = grid::Direction::North;
let mut leave = false;
let obstruction = (usize::MAX, usize::MAX);
let mut normal_walk = HashSet::new();
while !leave {
normal_walk.insert((x, y));
leave = self.move_guard(&mut x, &mut y, &mut direction, obstruction);
}
let mut stuck = 0;
for (xy, c) in self.grid.iter() {
// optimization: if the guard never walks to this position,
// an obstruction cannot deviate him
if !normal_walk.contains(&xy) {
continue;
}
if c == &'.' {
// can choose this position for the obstruction
let obstruction = xy;
let (mut x, mut y) = self.start;
let mut direction = grid::Direction::North;
let mut leave = false;
let mut seen: HashSet<(usize, usize, Direction)> = HashSet::new();
while !leave {
if seen.contains(&(x, y, direction)) {
// same pos, same direction : the guard is stuck
stuck += 1;
break;
}
seen.insert((x, y, direction));
leave = self.move_guard(&mut x, &mut y, &mut direction, obstruction);
}
}
}
stuck
}
}
fn main() {
let args = aoc::parse_args();
let mut puzzle = Puzzle::new();
puzzle.configure(args.path.as_str());
println!("{}", puzzle.part1());
println!("{}", puzzle.part2());
}
/// Test from puzzle input
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test01() {
let mut puzzle = Puzzle::new();
puzzle.configure("test.txt");
assert_eq!(puzzle.part1(), 41);
}
#[test]
fn test02() {
let mut puzzle = Puzzle::new();
puzzle.configure("test.txt");
assert_eq!(puzzle.part2(), 6);
}
}