-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday17.rs
175 lines (165 loc) · 4.78 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
use std::cmp::Reverse;
use std::collections::{BinaryHeap, HashMap};
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
enum Direction {
U,
L,
D,
R,
}
impl Direction {
fn next(&self) -> Direction {
match self {
Direction::U => Direction::L,
Direction::L => Direction::D,
Direction::D => Direction::R,
Direction::R => Direction::U,
}
}
fn prev(&self) -> Direction {
match self {
Direction::U => Direction::R,
Direction::L => Direction::U,
Direction::D => Direction::L,
Direction::R => Direction::D,
}
}
}
fn step(y: usize, x: usize, dir: Direction) -> Option<(usize, usize)> {
Some(match dir {
Direction::U => (y.checked_sub(1)?, x),
Direction::L => (y, x.checked_sub(1)?),
Direction::D => (y.checked_add(1)?, x),
Direction::R => (y, x.checked_add(1)?),
})
}
fn solve<P, F, T>(data: &str, ok: P, next: F) -> Option<usize>
where
P: Fn(usize) -> bool,
F: Fn(Direction, usize) -> T,
T: IntoIterator,
T::Item: Into<Direction>,
{
let maze = data
.lines()
.filter_map(|line| {
line.chars()
.map(|c| c.to_digit(10).filter(|&d| d > 0))
.collect::<Option<Vec<_>>>()
})
.collect::<Vec<_>>();
let mut queue: BinaryHeap<_> = [(Reverse(0), 0, 0, Direction::R, 0)].into();
let mut costs: HashMap<_, _> = [((0, 0, Direction::R, 0), 0)].into();
while let Some((Reverse(cost), y, x, direction @ direction0, distance)) = queue.pop() {
let cost = cost + y + x;
if y == maze.len() - 1 && x == maze[y].len() - 1 && ok(distance) {
return Some(cost);
}
match costs.entry((y, x, direction, distance)) {
std::collections::hash_map::Entry::Occupied(entry) => {
if *entry.get() < cost {
continue;
}
}
std::collections::hash_map::Entry::Vacant(_) => {
#[cfg(debug_assertions)]
panic!(
"missing state for ({}, {}, {}, {:?}, {})",
cost, y, x, direction, distance
);
}
}
for direction in next(direction, distance) {
let direction = direction.into();
let Some((y, x)) =
step(y, x, direction).filter(|&(y, x)| y < maze.len() && x < maze[y].len())
else {
continue;
};
let distance = if direction == direction0 {
distance + 1
} else {
1
};
let cost = cost + maze[y][x] as usize;
match costs.entry((y, x, direction, distance)) {
std::collections::hash_map::Entry::Occupied(mut entry) => {
if *entry.get() <= cost {
continue;
}
entry.insert(cost);
}
std::collections::hash_map::Entry::Vacant(entry) => {
entry.insert(cost);
}
}
queue.push((Reverse(cost - y - x), y, x, direction, distance));
}
}
None
}
pub fn part1(data: &str) -> Option<usize> {
solve(
data,
|_| true,
|direction, distance| {
if distance < 3 {
vec![direction.prev(), direction.next(), direction]
} else {
vec![direction.prev(), direction.next()]
}
},
)
}
pub fn part2(data: &str) -> Option<usize> {
solve(
data,
|distance| distance >= 4,
|direction, distance| {
if distance < 4 {
vec![direction]
} else if distance < 10 {
vec![direction.prev(), direction.next(), direction]
} else {
vec![direction.prev(), direction.next()]
}
},
)
}
#[cfg(test)]
mod tests {
use super::*;
use indoc::indoc;
use pretty_assertions::assert_eq;
static EXAMPLE_1: &str = indoc! {"
2413432311323
3215453535623
3255245654254
3446585845452
4546657867536
1438598798454
4457876987766
3637877979653
4654967986887
4564679986453
1224686865563
2546548887735
4322674655533
"};
static EXAMPLE_2: &str = indoc! {"
111111111111
999999999991
999999999991
999999999991
999999999991
"};
#[test]
fn part1_examples() {
assert_eq!(Some(102), part1(EXAMPLE_1));
}
#[test]
fn part2_examples() {
assert_eq!(Some(94), part2(EXAMPLE_1));
assert_eq!(Some(71), part2(EXAMPLE_2));
}
}