This repository was archived by the owner on Aug 1, 2024. It is now read-only.
generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path17.rs
164 lines (131 loc) · 4.25 KB
/
17.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
use std::collections::{HashSet, BinaryHeap};
use std::cmp::Ordering;
advent_of_code::solution!(17);
#[derive(PartialEq, Eq, Hash, Clone, Copy)]
enum Direction {
North,
West,
South,
East
}
impl Direction {
fn opposite(&self) -> Direction {
match self {
Self::North => Self::South,
Self::West => Self::East,
Self::South => Self::North,
Self::East => Self::West
}
}
}
#[derive(PartialEq, Eq, Clone, Copy, Hash)]
struct Coord {
x: usize,
y: usize
}
type Map = Vec<Vec<u32>>;
#[derive(PartialEq, Eq, Clone)]
struct Crucible {
loss: u32,
pos: Coord,
dir: Direction,
moves: usize,
}
impl Crucible {
fn move_dir(&self, dir: Direction, map: &Map) -> Option<Crucible> {
let (width, height) = (map[0].len(), map.len());
let new_coords = match dir {
Direction::North if self.pos.y > 0 =>
Some(Coord { x: self.pos.x, y: self.pos.y - 1 }),
Direction::West if self.pos.x > 0 =>
Some(Coord { x: self.pos.x - 1, y: self.pos.y }),
Direction::South if self.pos.y + 1 < height =>
Some(Coord { x: self.pos.x, y: self.pos.y + 1 }),
Direction::East if self.pos.x + 1 < width =>
Some(Coord { x: self.pos.x + 1, y: self.pos.y }),
_ => None
};
new_coords.map(|Coord { x, y }|
Crucible {
loss: self.loss + map[y][x],
pos: Coord { x, y },
dir,
moves: if self.dir == dir { self.moves + 1 } else { 1 },
})
}
}
impl Ord for Crucible {
fn cmp(&self, other: &Self) -> Ordering {
other.loss.cmp(&self.loss)
}
}
impl PartialOrd for Crucible {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
fn parse(input: &str) -> Map {
input.lines()
.map(|line| line.chars().map(|c| c.to_digit(10).unwrap()).collect())
.collect()
}
fn neighbours(crucible: Crucible, map: &Map, min_moves: usize, max_moves: usize) -> Vec<Crucible> {
let Crucible { dir, moves, .. } = crucible;
let mut ns: Vec<Option<Crucible>> = vec![];
for d in vec![Direction::North, Direction::West, Direction::South, Direction::East] {
if moves >= max_moves && dir == d {
continue;
}
if moves < min_moves && dir != d {
continue;
}
if dir.opposite() == d {
continue;
}
ns.push(crucible.move_dir(d, map));
}
ns.into_iter().filter_map(|x| x).collect()
}
fn find_path(start: Crucible, goal: Coord, map: &Map, min_moves: usize, max_moves: usize) -> u32 {
let mut visited: HashSet<(Coord, Direction, usize)> = HashSet::new();
let mut queue = BinaryHeap::new();
queue.push(start.move_dir(Direction::East, &map).unwrap());
queue.push(start.move_dir(Direction::South, &map).unwrap());
while let Some(current) = queue.pop() {
if current.pos == goal && current.moves >= min_moves {
return current.loss;
}
for c in neighbours(current, &map, min_moves, max_moves) {
if visited.insert((c.pos, c.dir, c.moves)) {
queue.push(c);
}
}
}
panic!("No path found")
}
pub fn part_one(input: &str) -> Option<u32> {
let data = parse(input);
let crucible = Crucible { loss: 0, pos: Coord { x: 0, y: 0 }, dir: Direction::North, moves: 1 };
let goal = Coord { x: data[0].len() - 1, y: data.len() - 1 };
find_path(crucible, goal, &data, 0, 3).into()
}
pub fn part_two(input: &str) -> Option<u32> {
let data = parse(input);
let crucible = Crucible { loss: 0, pos: Coord { x: 0, y: 0 }, dir: Direction::North, moves: 1 };
let goal = Coord { x: data[0].len() - 1, y: data.len() - 1 };
find_path(crucible, goal, &data, 4, 10).into()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(102));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(94));
}
}