-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday16.rs
145 lines (125 loc) · 3.64 KB
/
day16.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
use crate::solutions::Solution;
use crate::utils::direction::Direction::East;
use crate::utils::graphs::dijkstra::Dijkstra;
use crate::utils::grid::Grid;
use crate::utils::point::Point;
use crate::utils::vector::Vector;
use itertools::Itertools;
pub struct Day16;
impl Solution for Day16 {
fn part_one(&self, input: &str) -> String {
let (grid, start, end) = Self::setup_grid(input);
let dijkstra = Self::create_dijkstra(grid);
let is_end = Self::is_end_closure(end);
dijkstra.cost(vec![start], &is_end).unwrap().to_string()
}
fn part_two(&self, input: &str) -> String {
let (grid, start, end) = Self::setup_grid(input);
let dijkstra = Self::create_dijkstra(grid);
let is_end = Self::is_end_closure(end);
dijkstra
.all_paths(vec![start], &is_end)
.iter()
.flat_map(|path| path.iter().map(|p| p.position()))
.unique()
.count()
.to_string()
}
}
impl Day16 {
fn setup_grid(input: &str) -> (Grid<char>, Vector, Point) {
let grid: Grid<char> = Grid::from(input);
let start = grid.get_first_position(&'S').unwrap();
let start = Vector::new(start, East);
let end = grid.get_first_position(&'E').unwrap();
(grid, start, end)
}
fn create_dijkstra(grid: Grid<char>) -> Dijkstra<Vector> {
let adjacency = Self::adjacency_closure(grid);
let cost = Self::cost_closure();
Dijkstra::new(adjacency, cost)
}
fn adjacency_closure(grid: Grid<char>) -> Box<dyn Fn(Vector) -> Vec<Vector>> {
Box::new(move |vector: Vector| {
let vectors = vec![
vector.forward(),
vector.rotate_cw().forward(),
vector.rotate_ccw().forward(),
];
vectors
.into_iter()
.filter(|vec| {
grid.get_for_point(&vec.position())
.is_some_and(|element| ['.', 'E'].contains(element))
})
.collect_vec()
})
}
fn cost_closure() -> Box<dyn Fn(Vector, Vector) -> usize> {
Box::new(
move |current: Vector, next: Vector| {
if current.forward() == next {
1
} else {
1001
}
},
)
}
fn is_end_closure(end: Point) -> Box<dyn Fn(Vector) -> bool> {
Box::new(move |vector: Vector| vector.position() == end)
}
}
#[cfg(test)]
mod tests {
use crate::solutions::year2024::day16::Day16;
use crate::solutions::Solution;
const FIRST_EXAMPLE: &str = r#"###############
#.......#....E#
#.#.###.#.###.#
#.....#.#...#.#
#.###.#####.#.#
#.#.#.......#.#
#.#.#####.###.#
#...........#.#
###.#.#####.#.#
#...#.....#.#.#
#.#.#.###.#.#.#
#.....#...#.#.#
#.###.#.#.#.#.#
#S..#.....#...#
###############"#;
#[test]
fn part_one_example_1() {
assert_eq!("7036", Day16.part_one(FIRST_EXAMPLE));
}
#[test]
fn part_two_example_1() {
assert_eq!("45", Day16.part_two(FIRST_EXAMPLE));
}
const SECOND_EXAMPLE: &str = r#"#################
#...#...#...#..E#
#.#.#.#.#.#.#.#.#
#.#.#.#...#...#.#
#.#.#.#.###.#.#.#
#...#.#.#.....#.#
#.#.#.#.#.#####.#
#.#...#.#.#.....#
#.#.#####.#.###.#
#.#.#.......#...#
#.#.###.#####.###
#.#.#...#.....#.#
#.#.#.#####.###.#
#.#.#.........#.#
#.#.#.#########.#
#S#.............#
#################"#;
#[test]
fn part_one_example_2() {
assert_eq!("11048", Day16.part_one(SECOND_EXAMPLE));
}
#[test]
fn part_two_example_2() {
assert_eq!("64", Day16.part_two(SECOND_EXAMPLE));
}
}