generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path18.rs
182 lines (151 loc) · 4.66 KB
/
18.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
176
177
178
179
180
181
182
use std::collections::BinaryHeap;
use advent_of_code::util::{
direction::DIRECTIONS,
grid::{GridGetPoint as _, GridGetPointMut as _},
point::Point2D,
DistanceState,
};
use grid::Grid;
advent_of_code::solution!(18);
pub fn part_one(input: &str) -> Option<u64> {
part_one_inner(input, 71, 71, 1024)
}
fn part_one_inner(input: &str, width: usize, height: usize, fallen: usize) -> Option<u64> {
let bytes = parse_input(input);
let grid = build_grid(width, height, &bytes[0..fallen]);
shortest_path_length(
&grid,
(0, 0).into(),
(width as isize - 1, height as isize - 1).into(),
)
}
pub fn part_two(input: &str) -> Option<String> {
part_two_inner(input, 71, 71, 1024)
}
fn part_two_inner(input: &str, width: usize, height: usize, previsit: usize) -> Option<String> {
let bytes = parse_input(input);
let mut grid = Grid::init(height, width, true);
let start = (0, 0).into();
let end = (width as isize - 1, height as isize - 1).into();
for byte in &bytes[0..previsit] {
*grid.point_mut(*byte).unwrap() = false;
}
let (_, mut visited) = has_path(&grid, start, end);
for &byte in &bytes[previsit..] {
*grid.point_mut(byte).unwrap() = false;
if *visited.point(byte).unwrap() {
let (has, new_visited) = has_path(&grid, start, end);
if !has {
return Some(format!("{},{}", byte.x(), byte.y()));
}
visited = new_visited;
}
}
None
}
fn shortest_path_length(
grid: &Grid<bool>,
start: Point2D<isize>,
end: Point2D<isize>,
) -> Option<u64> {
let mut visited_distances = Grid::init(grid.rows(), grid.cols(), None);
let mut visit = BinaryHeap::new();
*visited_distances.point_mut(start).unwrap() = Some(0);
visit.push(DistanceState::new(0, start));
while let Some(DistanceState {
distance,
state: location,
}) = visit.pop()
{
if location == end {
return Some(distance);
}
for &dir in DIRECTIONS.iter() {
let new_loc = location + dir.into();
if new_loc.x() < 0
|| new_loc.y() < 0
|| new_loc.x() >= grid.cols() as isize
|| new_loc.y() >= grid.rows() as isize
{
continue;
}
if !*grid.point(new_loc).unwrap() {
continue;
}
let new_distance = distance + 1;
let visited_distance = visited_distances.point_mut(new_loc).unwrap();
if visited_distance.is_none() || new_distance < visited_distance.unwrap() {
*visited_distance = Some(new_distance);
visit.push(DistanceState::new(new_distance, new_loc));
}
}
}
None
}
fn has_path(grid: &Grid<bool>, start: Point2D<isize>, end: Point2D<isize>) -> (bool, Grid<bool>) {
let mut visited = Grid::init(grid.rows(), grid.cols(), false);
let mut stack = vec![start];
while let Some(loc) = stack.pop() {
if loc.x() < 0
|| loc.y() < 0
|| loc.x() >= grid.cols() as isize
|| loc.y() >= grid.rows() as isize
{
continue;
}
if !*grid.point(loc).unwrap() || *visited.point(loc).unwrap() {
continue;
}
if loc == end {
return (true, visited);
}
*visited.point_mut(loc).unwrap() = true;
DIRECTIONS
.iter()
.rev()
.for_each(|&dir| stack.push(loc + dir.into()));
}
(false, visited)
}
fn build_grid(width: usize, height: usize, bytes: &[Point2D<isize>]) -> Grid<bool> {
let mut grid = Grid::init(width, height, true);
for byte in bytes {
*grid.point_mut(*byte).unwrap() = false;
let (x, y) = (byte.x(), byte.y());
*grid.point_mut((x, y)).unwrap() = false;
}
grid
}
fn parse_input(input: &str) -> Vec<Point2D<isize>> {
input
.lines()
.map(|line| {
let (x, y) = line.split_once(",").unwrap();
Point2D::new(x.parse().unwrap(), y.parse().unwrap())
})
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let result = part_one_inner(
&advent_of_code::template::read_file("examples", DAY),
7,
7,
12,
);
assert_eq!(result, Some(22));
}
#[test]
fn test_part_two() {
let result = part_two_inner(
&advent_of_code::template::read_file("examples", DAY),
7,
7,
12,
);
assert_eq!(result, Some("6,1".to_owned()));
}
}