-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday18.rs
137 lines (124 loc) · 3.32 KB
/
day18.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
use aoc_runner_derive::aoc;
#[aoc(day18, part1)]
#[must_use]
pub fn part1(input: &str) -> usize {
Memory::<71>::solve1(input)
}
#[aoc(day18, part2)]
#[must_use]
pub fn part2(input: &str) -> String {
Memory::<71>::solve2(input)
}
#[repr(transparent)]
#[derive(derive_more::Deref, derive_more::DerefMut)]
struct Memory<const D: usize>(nalgebra::SMatrix<u16, D, D>);
impl<const D: usize> Memory<D> {
#[inline]
fn solve1(input: &str) -> usize {
Memory::<D>::parse(input).search(1024).unwrap()
}
#[inline]
fn solve2(input: &str) -> String {
let mut mem = Memory::<D>::parse(input);
let (mut min, mut max) = (0, 2 << 12);
while min < max {
let cost = (min + max) / 2;
if mem.search(cost).is_some() {
min = unsafe { cost.unchecked_add(1) };
} else {
max = cost;
}
}
mem.iter()
.position(|&time| time == min)
.map(|i| {
let (x, y) = (i % D, i / D);
format!("{x},{y}")
})
.unwrap()
}
#[inline]
fn parse(input: &str) -> Self {
let mut mem = nalgebra::SMatrix::from_element(u16::MAX);
input
.split('\n')
.filter(|s| !s.is_empty())
.enumerate()
.for_each(|(i, coordinates)| unsafe {
let (x, y) = coordinates.split_once(',').unwrap_unchecked();
let (x, y) = (x.parse().unwrap_unchecked(), y.parse().unwrap_unchecked());
mem[(x, y)] = i as u16;
});
Self(mem)
}
#[inline]
fn search(&mut self, n_bytes: u16) -> Option<usize> {
let mut seen = **self;
seen[(0, 0)] = 0;
let mut queue = std::collections::VecDeque::with_capacity(52);
queue.push_back(((0, 0), 0));
while let Some((pos, cost)) = queue.pop_front() {
if pos == (D - 1, D - 1) {
return Some(cost);
}
[(0, 1), (1, 0), (0, -1), (-1, 0)]
.into_iter()
.filter_map(|(dx, dy)| {
let new_x = pos.0.wrapping_add_signed(dx);
(new_x < D)
.then(|| {
let new_y = pos.1.wrapping_add_signed(dy);
(new_y < D).then_some((new_x, new_y))
})
.flatten()
})
.for_each(|new_pos| {
if seen[new_pos] > n_bytes {
queue.push_back((new_pos, unsafe { cost.unchecked_add(1) }));
seen[new_pos] = 0;
}
});
}
None
}
}
#[cfg(test)]
mod tests {
use super::*;
use indoc::indoc;
const SAMPLE: &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]
pub fn part1_example() {
assert_eq!(Memory::<9>::solve1(SAMPLE), 22);
}
#[test]
pub fn part2_example() {
assert_eq!(Memory::<7>::solve2(SAMPLE), "6,1");
}
}