-
Notifications
You must be signed in to change notification settings - Fork 0
/
day06.rs
143 lines (113 loc) · 4.06 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
use crate::solutions::Solution;
use crate::utils::direction::Direction::North;
use crate::utils::grid::Grid;
use crate::utils::point::Point;
use crate::utils::surface_range::SurfaceRange;
use crate::utils::vector::Vector;
use std::collections::HashSet;
pub struct Day06;
const OBSTRUCTION: char = '#';
const STARTING_POSITION: char = '^';
impl Solution for Day06 {
fn part_one(&self, input: &str) -> String {
let grid: Grid<char> = Grid::from(input);
let obstructions = grid.get_all_positions(&OBSTRUCTION);
let starting_point = grid.get_first_position(&STARTING_POSITION).unwrap();
let surface = grid.surface();
let mut guard = Vector::new(starting_point, North);
let mut visited_positions: HashSet<Point> = HashSet::new();
while surface.contains(guard.position()) {
visited_positions.insert(guard.position());
guard = self.next_step(guard, &obstructions);
}
visited_positions.len().to_string()
}
fn part_two(&self, input: &str) -> String {
let grid: Grid<char> = Grid::from(input);
let obstructions = grid.get_all_positions(&OBSTRUCTION);
let starting_point = grid.get_first_position(&STARTING_POSITION).unwrap();
let surface = grid.surface();
let mut guard = Vector::new(starting_point, North);
let mut visited_positions: HashSet<Point> = HashSet::new();
let mut loop_count: u32 = 0;
while surface.contains(guard.position()) {
if starting_point != guard.position()
&& !visited_positions.contains(&guard.position())
&& self.does_it_loop(guard, &obstructions, &surface)
{
loop_count += 1;
}
visited_positions.insert(guard.position());
guard = self.next_step(guard, &obstructions);
}
loop_count.to_string()
}
}
impl Day06 {
fn next_step(&self, guard: Vector, obstructions: &[Point]) -> Vector {
let mut next_position = guard;
while obstructions.contains(&next_position.forward().position()) {
next_position = next_position.rotate_cw();
}
next_position.forward()
}
fn does_it_loop(&self, guard: Vector, obstructions: &[Point], surface: &SurfaceRange) -> bool {
let mut visited_positions: Vec<Vector> = Vec::new();
let mut obstructions = obstructions.to_owned();
obstructions.push(guard.position());
let mut guard = guard.backward();
loop {
visited_positions.push(guard);
guard = self.next_step(guard, &obstructions);
if visited_positions.contains(&guard) {
return true;
}
if !surface.contains(guard.position()) {
return false;
}
}
}
}
#[cfg(test)]
mod tests {
use crate::solutions::year2024::day06::{Day06, OBSTRUCTION, STARTING_POSITION};
use crate::solutions::Solution;
use crate::utils::direction::Direction::{North, South};
use crate::utils::grid::Grid;
use crate::utils::point::Point;
use crate::utils::vector::Vector;
const EXAMPLE: &str = r#"....#.....
.........#
..........
..#.......
.......#..
..........
.#..^.....
........#.
#.........
......#..."#;
#[test]
fn part_one_example_test() {
assert_eq!("41", Day06.part_one(EXAMPLE));
}
#[test]
fn part_two_example_test() {
assert_eq!("6", Day06.part_two(EXAMPLE));
}
#[test]
fn make_step_in_corner() {
let map = r#".....
#....
.#...
^...."#;
let grid: Grid<char> = Grid::from(map);
let obstructions = grid.get_all_positions(&OBSTRUCTION);
let starting_position = grid.get_first_position(&STARTING_POSITION).unwrap();
let guard = Vector::new(starting_position, North);
let day = Day06;
let first = day.next_step(guard, &obstructions);
assert_eq!(first, Vector::new(Point::new(0, 2), North));
let second = day.next_step(first, &obstructions);
assert_eq!(second, Vector::new(Point::new(0, 3), South))
}
}