-
Notifications
You must be signed in to change notification settings - Fork 0
/
day15.rs
165 lines (136 loc) · 3.46 KB
/
day15.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
//! [Day 15: Chiton](https://adventofcode.com/2021/day/15)
use std::collections::BinaryHeap;
struct Cost {
cost: u32,
x: usize,
y: usize,
}
impl Ord for Cost {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
other.cost.cmp(&self.cost)
}
}
impl PartialOrd for Cost {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl PartialEq for Cost {
fn eq(&self, other: &Self) -> bool {
self.cost == other.cost
}
}
impl Eq for Cost {}
fn min_cost(grid: &[Vec<u32>]) -> u32 {
let n = grid.len();
let mut d = vec![vec![0u32; n]; n];
let mut heap = BinaryHeap::new();
heap.push(Cost {
cost: 0,
x: 0,
y: 0,
});
while !heap.is_empty() {
let cur = heap.pop().unwrap();
let new_cost = cur.cost + grid[cur.y][cur.x];
if new_cost >= d[cur.y][cur.x] && d[cur.y][cur.x] != 0 {
continue;
}
d[cur.y][cur.x] = new_cost;
if cur.x == n - 1 && cur.y == n - 1 {
break;
}
if cur.x + 1 < n {
heap.push(Cost {
cost: new_cost,
x: cur.x + 1,
y: cur.y,
});
}
if cur.y + 1 < n {
heap.push(Cost {
cost: new_cost,
x: cur.x,
y: cur.y + 1,
});
}
if cur.x > 0 {
heap.push(Cost {
cost: new_cost,
x: cur.x - 1,
y: cur.y,
});
}
if cur.y > 0 {
heap.push(Cost {
cost: new_cost,
x: cur.x,
y: cur.y - 1,
});
}
}
d[n - 1][n - 1]
}
struct Puzzle {
grid: Vec<Vec<u32>>,
}
impl Puzzle {
fn new() -> Puzzle {
Puzzle { grid: vec![] }
}
/// Get the puzzle input.
fn configure(&mut self, path: &str) {
let data = std::fs::read_to_string(path).unwrap();
// read the grid
for line in data.lines() {
let row: Vec<_> = line.chars().filter_map(|c| c.to_digit(10)).collect();
self.grid.push(row);
}
}
/// Solve part one.
fn part1(&self) -> u32 {
min_cost(&self.grid) - self.grid[0][0]
}
/// Solve part two.
fn part2(&self) -> u32 {
let n = self.grid.len();
// build the five times larger grid
let mut grid5 = vec![vec![0u32; 5 * n]; 5 * n];
for y in 0..n {
for x in 0..n {
let v = self.grid[y][x];
for yy in 0..5 {
for xx in 0..5 {
grid5[y + n * yy][x + n * xx] =
(v - 1 + u32::try_from(xx + yy).unwrap()) % 9 + 1;
}
}
}
}
min_cost(&grid5) - self.grid[0][0]
}
}
fn main() {
let args = aoc::parse_args();
let mut puzzle = Puzzle::new();
puzzle.configure(args.path.as_str());
println!("{}", puzzle.part1());
println!("{}", puzzle.part2());
}
/// Test from puzzle input
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test01() {
let mut puzzle = Puzzle::new();
puzzle.configure("sample_1.txt");
assert_eq!(puzzle.part1(), 40);
}
#[test]
fn test02() {
let mut puzzle = Puzzle::new();
puzzle.configure("sample_1.txt");
assert_eq!(puzzle.part2(), 315);
}
}