-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathday14.rs
144 lines (134 loc) · 3.6 KB
/
day14.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
use super::util::iter_pairs;
use std::cmp::{max, min};
struct Bitmap {
data: Vec<bool>,
width: usize,
}
impl Bitmap {
fn new(width: usize, height: usize) -> Self {
Bitmap {
data: vec![false; width * height],
width,
}
}
fn contains(&self, x: usize, y: usize) -> bool {
assert!((0..self.width).contains(&x));
self.data[x + y * self.width]
}
fn add(&mut self, x: usize, y: usize) {
assert!((0..self.width).contains(&x));
self.data[x + y * self.width] = true;
}
}
fn parse<'a, I, S>(lines: I) -> Option<(Bitmap, usize)>
where
I: IntoIterator<Item = &'a S>,
S: AsRef<str> + 'a,
{
let segments = lines
.into_iter()
.flat_map(|line| {
iter_pairs(line.as_ref().split(" -> ").filter_map(|pair| {
let (x, y) = pair.split_once(',')?;
Some((x.parse().ok()?, y.parse().ok()?))
}))
})
.collect::<Vec<_>>();
let max_x = segments
.iter()
.map(|((x1, _), (x2, _))| max(*x1, *x2))
.max()?;
let max_y = segments
.iter()
.map(|((_, y1), (_, y2))| max(*y1, *y2))
.max()?;
let width = max(max_x + 1, 500 + max_y + 2);
let mut bitmap = Bitmap::new(width, max_y + 2);
for ((x1, y1), (x2, y2)) in segments {
for x in min(x1, x2)..=max(x1, x2) {
for y in min(y1, y2)..=max(y1, y2) {
bitmap.add(x, y)
}
}
}
Some((bitmap, max_y))
}
enum FillState {
Pre(usize, usize),
Post(usize, usize),
}
struct FillIterator {
blocks: Bitmap,
max_y: usize,
stack: Vec<FillState>,
}
impl FillIterator {
fn new(blocks: Bitmap, max_y: usize) -> Self {
FillIterator {
blocks,
max_y,
stack: vec![FillState::Pre(500, 0)],
}
}
}
impl Iterator for FillIterator {
type Item = (usize, usize);
fn next(&mut self) -> Option<Self::Item> {
loop {
let state = self.stack.pop()?;
match state {
FillState::Pre(x, y) => {
if self.blocks.contains(x, y) {
continue;
}
self.stack.push(FillState::Post(x, y));
if y <= self.max_y {
self.stack.extend([
FillState::Pre(x + 1, y + 1),
FillState::Pre(x - 1, y + 1),
FillState::Pre(x, y + 1),
]);
}
}
FillState::Post(x, y) => {
self.blocks.add(x, y);
return Some((x, y));
}
}
}
}
}
pub fn both_parts<'a, I, S>(lines: I) -> (usize, usize)
where
I: IntoIterator<Item = &'a S>,
S: AsRef<str> + 'a,
{
let Some((blocks, max_y)) = parse(lines) else { return (0, 0) };
FillIterator::new(blocks, max_y)
.enumerate()
.scan(None, |part1, (i, (_, y))| {
if y >= max_y && part1.is_none() {
*part1 = Some(i);
}
Some((part1.unwrap_or(i + 1), i + 1))
})
.last()
.unwrap_or((0, 0))
}
#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;
static EXAMPLE: &[&str] = &[
"498,4 -> 498,6 -> 496,6",
"503,4 -> 502,4 -> 502,9 -> 494,9",
];
#[test]
fn part1_examples() {
assert_eq!(24, both_parts(EXAMPLE).0);
}
#[test]
fn part2_examples() {
assert_eq!(93, both_parts(EXAMPLE).1);
}
}