-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday21.rs
56 lines (44 loc) · 1.37 KB
/
day21.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
use crate::solutions::Solution;
use crate::utils::grid::Grid;
use crate::utils::point::Point;
use std::collections::HashSet;
pub struct Day21;
impl Solution for Day21 {
fn part_one(&self, input: &str) -> String {
Self::steps(input, 64)
}
fn part_two(&self, _input: &str) -> String {
String::from('0')
}
}
impl Day21 {
fn steps(input: &str, count: usize) -> String {
let grid: Grid<char> = Grid::from(input);
let start = grid.get_first_position(&'S').unwrap();
let surface = grid.surface();
let rocks = grid.get_all_positions(&'#');
let mut reached: HashSet<Point> = HashSet::from([start]);
for _ in 1..=count {
let mut new_reached: HashSet<Point> = HashSet::new();
for point in &reached {
for adj in point.adjacent() {
if surface.contains(adj) && !rocks.contains(&adj) {
new_reached.insert(adj);
}
}
}
reached = new_reached;
}
reached.len().to_string()
}
}
#[cfg(test)]
mod tests {
use crate::solutions::year2023::day21::Day21;
use crate::solutions::year2023::read_2023_example;
#[test]
fn part_one_example_test() {
let input = read_2023_example("21");
assert_eq!("16", Day21::steps(input.as_str(), 6));
}
}