-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday_22.rs
125 lines (108 loc) Β· 3.02 KB
/
day_22.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
use std::{collections::HashSet, convert::identity};
use common::{solution, Answer};
use nd_vec::{vector, Vec3};
solution!("Sand Slabs", 22);
fn part_a(input: &str) -> Answer {
solve(parse(input), false, |x| (x == 0) as u32).into()
}
fn part_b(input: &str) -> Answer {
solve(parse(input), true, identity).into()
}
fn solve(mut map: Vec<Box>, exhaustive: bool, count: fn(u32) -> u32) -> u32 {
// Shift all boxes down as far as possible
while shift_down(&mut map, false) != 0 {}
// For each box, remove it and shift all other boxes down as far as possible
let mut out = 0;
for i in 0..map.len() {
let mut map_clone = map.clone();
map_clone.remove(i);
out += count(shift_down(&mut map_clone, exhaustive));
}
out
}
fn shift_down(map: &mut [Box], exhaustive: bool) -> u32 {
let mut moved = HashSet::new();
let mut dirty = true;
while dirty {
dirty = false;
'outer: for i in 0..map.len() {
// If no other box below this one, move it down
let item = map[i];
for x in item.a.x()..=item.b.x() {
for y in item.a.y()..=item.b.y() {
if item.a.z() == 1
|| map
.iter()
.take(i)
.rev()
.any(|b| b.contains(vector!(x, y, item.a.z() - 1)))
{
continue 'outer;
}
}
}
map[i].a -= vector!(0, 0, 1);
map[i].b -= vector!(0, 0, 1);
moved.insert(i);
dirty = exhaustive;
}
}
moved.len() as u32
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
struct Box {
a: Vec3<u32>,
b: Vec3<u32>,
}
impl Box {
fn contains(&self, p: Vec3<u32>) -> bool {
p.x() >= self.a.x()
&& p.x() <= self.b.x()
&& p.y() >= self.a.y()
&& p.y() <= self.b.y()
&& p.z() >= self.a.z()
&& p.z() <= self.b.z()
}
}
fn parse(input: &str) -> Vec<Box> {
let mut out = Vec::new();
for line in input.lines() {
let parse = |s: &str| {
let a = s.split(',').collect::<Vec<_>>();
vector!(
a[0].parse().unwrap(),
a[1].parse().unwrap(),
a[2].parse().unwrap()
)
};
let (a, b) = line.split_once('~').unwrap();
let (a, b) = (parse(a), parse(b));
out.push(Box {
a: a.min(&b),
b: a.max(&b),
});
}
out.sort_unstable_by_key(|b| b.a.z());
out
}
#[cfg(test)]
mod test {
use indoc::indoc;
const CASE: &str = indoc! {"
1,0,1~1,2,1
0,0,2~2,0,2
0,2,3~2,2,3
0,0,4~0,2,4
2,0,5~2,2,5
0,1,6~2,1,6
1,1,8~1,1,9
"};
#[test]
fn part_a() {
assert_eq!(super::part_a(CASE), 5.into());
}
#[test]
fn part_b() {
assert_eq!(super::part_b(CASE), 7.into());
}
}