-
Notifications
You must be signed in to change notification settings - Fork 0
/
day10.rs
159 lines (136 loc) · 3.21 KB
/
day10.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
use crate::solutions::Solution;
use crate::utils::grid::Grid;
use crate::utils::point::Point;
use itertools::Itertools;
pub struct Day10;
impl Solution for Day10 {
fn part_one(&self, input: &str) -> String {
self.solve(input, |points| points.into_iter().unique().collect())
}
fn part_two(&self, input: &str) -> String {
self.solve(input, |points| points)
}
}
impl Day10 {
fn solve<F>(&self, input: &str, modify_points: F) -> String
where
F: Fn(Vec<Point>) -> Vec<Point>,
{
let grid = Grid::from_custom(input.trim(), |c| {
c.to_digit(10).map(|x| x as i16).unwrap_or(-1)
});
grid.get_all_positions(&0)
.iter()
.map(|&start| self.process_path(&grid, start, &modify_points))
.sum::<usize>()
.to_string()
}
fn process_path<F>(&self, grid: &Grid<i16>, start: Point, modify_points: &F) -> usize
where
F: Fn(Vec<Point>) -> Vec<Point>,
{
let mut current_points = vec![start];
for current in 0..9 {
let next = current + 1;
current_points = modify_points(
current_points
.iter()
.flat_map(|point| {
point.adjacent().into_iter().filter(|next_point| {
grid.get_for_point(next_point)
.is_some_and(|&value| value == next)
})
})
.collect(),
);
}
current_points.len()
}
}
#[cfg(test)]
mod tests {
use crate::solutions::year2024::day10::Day10;
use crate::solutions::Solution;
const EXAMPLE_1: &str = r#"..90..9
...1.98
...2..7
6543456
765.987
876....
987...."#;
const EXAMPLE_2: &str = r#"89010123
78121874
87430965
96549874
45678903
32019012
01329801
10456732"#;
#[test]
fn part_one_example_test_1() {
const FIRST_EXAMPLE: &str = r#"0123
1234
8765
9876"#;
assert_eq!("1", Day10.part_one(FIRST_EXAMPLE));
}
#[test]
fn part_one_example_test_2() {
const EXAMPLE: &str = r#"...0...
...1...
...2...
6543456
7.....7
8.....8
9.....9"#;
assert_eq!("2", Day10.part_one(EXAMPLE));
}
#[test]
fn part_one_example_test_3() {
assert_eq!("4", Day10.part_one(EXAMPLE_1));
}
#[test]
fn part_one_example_test_4() {
const EXAMPLE: &str = r#"10..9..
2...8..
3...7..
4567654
...8..3
...9..2
.....01"#;
assert_eq!("3", Day10.part_one(EXAMPLE));
}
#[test]
fn part_one_example_test_5() {
assert_eq!("36", Day10.part_one(EXAMPLE_2));
}
#[test]
fn part_two_example_test_1() {
const EXAMPLE: &str = r#".....0.
..4321.
..5..2.
..6543.
..7..4.
..8765.
..9...."#;
assert_eq!("3", Day10.part_two(EXAMPLE));
}
#[test]
fn part_two_example_test_2() {
assert_eq!("13", Day10.part_two(EXAMPLE_1));
}
#[test]
fn part_two_example_test_3() {
const EXAMPLE: &str = r#"012345
123456
234567
345678
4.6789
56789."#;
assert_eq!("227", Day10.part_two(EXAMPLE))
}
#[test]
fn part_two_example_test_4() {
assert_eq!("81", Day10.part_two(EXAMPLE_2));
}
}