-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday06.rs
102 lines (87 loc) · 2.64 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
use std::collections::HashSet;
use aoc_lib::{
answer::Answer,
directions::{Advance, Cardinal, Direction},
matrix::Matrix,
solution::Solution,
vec2::Vec2,
};
pub struct Day6;
impl Solution for Day6 {
fn part_a(&self, input: &[String]) -> Answer {
let map = parse(input);
map.seen().len().into()
}
fn part_b(&self, input: &[String]) -> Answer {
let map = parse(input);
map.seen().iter().filter(|p| map.is_loop(p)).count().into()
}
}
fn parse(input: &[String]) -> Map {
let grid = Matrix::from_chars(input);
let starting_pos = grid.find('^').unwrap();
Map { grid, starting_pos }
}
struct Map {
grid: Matrix<char>,
starting_pos: Vec2<usize>,
}
impl Map {
fn seen(&self) -> HashSet<Vec2<usize>> {
let mut pos = Vec2::<isize>::from(self.starting_pos);
let mut dir = Cardinal::North;
let mut visited: HashSet<Vec2<usize>> = HashSet::new();
loop {
visited.insert(Vec2::<usize>::try_from(&pos).unwrap());
let next = dir.advance(pos);
let Some(&ch) = self.grid.get(&next) else {
break;
};
if ch == '#' {
dir = dir.turn_right();
} else {
pos = next;
}
}
visited
}
fn is_loop(&self, obstacle: &Vec2<usize>) -> bool {
let mut pos = Vec2::<isize>::from(self.starting_pos);
let mut dir = Cardinal::North;
let mut visited: HashSet<(Vec2<usize>, Cardinal)> = HashSet::new();
loop {
let Some(&ch) = self.grid.get(&pos) else {
break;
};
if ch == '#' || pos == obstacle.into() {
pos = dir.opposite().advance(pos);
dir = dir.turn_right();
}
let current_state = (Vec2::<usize>::try_from(&pos).unwrap(), dir);
if !visited.insert(current_state) {
return true;
}
pos = dir.advance(pos);
}
false
}
}
#[cfg(test)]
mod test {
use aoc_lib::{answer::Answer, input, solution::Solution};
use super::Day6;
#[test]
fn test_a() {
let input =
input::read_file(&format!("{}day_06_test.txt", crate::FILES_PREFIX_TEST)).unwrap();
let answer = Day6.part_a(&input);
assert_eq!(<i32 as Into<Answer>>::into(41), answer);
}
#[test]
fn test_b() {
let input =
input::read_file(&format!("{}day_06_test.txt", crate::FILES_PREFIX_TEST)).unwrap();
let answer = Day6.part_b(&input);
assert_eq!(<i32 as Into<Answer>>::into(6), answer);
}
}