-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday_18.rs
139 lines (114 loc) Β· 2.88 KB
/
day_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
use std::collections::{HashSet, VecDeque};
use aoc_lib::{direction::cardinal::Direction, matrix::Grid};
use common::{solution, Answer};
use nd_vec::{vector, Vec2};
solution!("RAM Run", 18);
fn part_a(input: &str) -> Answer {
let mut map = Map::parse(input, vector!(71, 71));
map.fill_to(1023);
map.shortest_path().into()
}
fn part_b(input: &str) -> Answer {
let mut map = Map::parse(input, vector!(71, 71));
let (mut start, mut end) = (0, map.falling.len() - 1);
while start <= end {
let mid = (start + end) / 2;
let next = map.fill_to(mid);
let works = map.shortest_path() != usize::MAX;
if !works && {
map.fill_to(mid - 1);
map.shortest_path() != usize::MAX
} {
return format!("{},{}", next.x(), next.y()).into();
}
if works {
start = mid + 1;
} else {
end = mid - 1;
}
}
panic!("No solution found")
}
struct Map {
board: Grid<bool>,
falling: Vec<Vec2<usize>>,
}
impl Map {
fn parse(input: &str, size: Vec2<usize>) -> Self {
let falling = input
.lines()
.map(|x| {
let (a, b) = x.split_once(',').unwrap();
vector!(a.parse::<usize>().unwrap(), b.parse::<usize>().unwrap())
})
.collect::<Vec<_>>();
let board = Grid::new(size, false);
Self { falling, board }
}
fn fill_to(&mut self, end: usize) -> Vec2<usize> {
self.board.fill(false);
for ins in &self.falling[0..=end] {
self.board.set(*ins, true);
}
self.falling[end]
}
fn shortest_path(&self) -> usize {
let mut queue = VecDeque::new();
let mut seen = HashSet::new();
queue.push_back((vector!(0, 0), 0));
while let Some((pos, depth)) = queue.pop_front() {
if pos + vector!(1, 1) == self.board.size() {
return depth;
}
if !seen.insert(pos) {
continue;
}
for dir in Direction::ALL {
let next = dir.wrapping_advance(pos);
if self.board.get(next) == Some(&false) {
queue.push_back((next, depth + 1));
}
}
}
usize::MAX
}
}
#[cfg(test)]
mod test {
use indoc::indoc;
use nd_vec::vector;
use super::Map;
const CASE: &str = indoc! {"
5,4
4,2
4,5
3,0
2,1
6,3
2,4
1,5
0,6
3,3
2,6
5,1
1,2
5,5
2,5
6,5
1,4
0,4
6,4
1,1
6,1
1,0
0,5
1,6
2,0
"};
#[test]
fn part_a() {
let mut map = Map::parse(CASE, vector!(7, 7));
map.fill_to(11);
assert_eq!(map.shortest_path(), 22);
}
}