-
Notifications
You must be signed in to change notification settings - Fork 0
/
day17.rs
153 lines (125 loc) · 4.24 KB
/
day17.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
use crate::solutions::Solution;
use crate::utils::direction::Direction;
use crate::utils::graphs::dijkstra::Dijkstra;
use crate::utils::grid::Grid;
use crate::utils::point::Point;
use crate::utils::vector::Vector;
pub struct Day17;
impl Solution for Day17 {
fn part_one(&self, input: &str) -> String {
let grid: Grid<u8> = Self::parse(input);
let grid_clone = grid.clone();
let grid_clone_2 = grid.clone();
let adjacency = move |node: Node| -> Vec<Node> {
let vec: Vec<Node> = if node.forward_count < 3 {
vec![node.left(), node.right(), node.forward()]
} else {
vec![node.left(), node.right()]
};
Self::filter_out_outside_grid(vec, &grid_clone)
};
let is_end = move |node: Node| -> bool { Self::is_end_node(node, &grid_clone_2) };
Self::solve(&grid, Box::new(adjacency), Box::new(is_end))
}
fn part_two(&self, input: &str) -> String {
let grid: Grid<u8> = Self::parse(input);
let grid_clone = grid.clone();
let grid_clone_2 = grid.clone();
let adjacency = move |node: Node| -> Vec<Node> {
let vec: Vec<Node> = if node.forward_count < 4 {
vec![node.forward()]
} else if node.forward_count >= 4 && node.forward_count < 10 {
vec![node.forward(), node.left(), node.right()]
} else {
vec![node.left(), node.right()]
};
Self::filter_out_outside_grid(vec, &grid_clone)
};
let is_end = move |node: Node| -> bool {
node.forward_count >= 4 && Self::is_end_node(node, &grid_clone_2)
};
Self::solve(&grid, Box::new(adjacency), Box::new(is_end))
}
}
impl Day17 {
fn parse(input: &str) -> Grid<u8> {
Grid::from_custom(input, |c| c.to_digit(10).unwrap() as u8)
}
fn solve(
grid: &Grid<u8>,
adjacency: Box<dyn Fn(Node) -> Vec<Node>>,
is_end: Box<dyn Fn(Node) -> bool>,
) -> String {
let start_point = grid.surface().top_left_corner();
let grid_clone = grid.clone();
let cost = move |_, next: Node| {
*grid_clone.get_for_point(&next.vector.position()).unwrap() as usize
};
let dijkstra: Dijkstra<Node> = Dijkstra::new(adjacency, Box::new(cost));
let starts = vec![
Node::new(start_point, Direction::East),
Node::new(start_point, Direction::South),
];
dijkstra.cost(starts, &is_end).unwrap().to_string()
}
fn filter_out_outside_grid(vec: Vec<Node>, grid: &Grid<u8>) -> Vec<Node> {
vec.into_iter()
.filter(|n| grid.surface().contains(n.vector.position()))
.collect()
}
fn is_end_node(node: Node, grid: &Grid<u8>) -> bool {
node.vector.position() == grid.surface().bottom_right_corner()
}
}
#[derive(Clone, Copy, Hash, Debug, Eq, PartialEq, PartialOrd, Ord)]
struct Node {
vector: Vector,
forward_count: u8,
}
impl Node {
fn new(position: Point, direction: Direction) -> Self {
Self {
vector: Vector::new(position, direction),
forward_count: 0,
}
}
fn forward(&self) -> Self {
Self {
vector: self.vector.forward(),
forward_count: self.forward_count + 1,
}
}
fn left(&self) -> Self {
Self {
vector: self.vector.rotate_ccw().forward(),
forward_count: 1,
}
}
fn right(&self) -> Self {
Self {
vector: self.vector.rotate_cw().forward(),
forward_count: 1,
}
}
}
#[cfg(test)]
mod tests {
use crate::solutions::year2023::day17::Day17;
use crate::solutions::year2023::read_2023_example;
use crate::solutions::Solution;
#[test]
fn part_one_example_test() {
let input = read_2023_example("17");
assert_eq!("102", Day17.part_one(input.as_str()));
}
#[test]
fn part_two_example_test() {
let input = read_2023_example("17");
assert_eq!("94", Day17.part_two(input.as_str()));
}
#[test]
fn part_two_example_2_test() {
let input = read_2023_example("17_2");
assert_eq!("71", Day17.part_two(input.as_str()));
}
}