-
Notifications
You must be signed in to change notification settings - Fork 0
/
d22.rs
209 lines (176 loc) · 5.01 KB
/
d22.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#![allow(
clippy::must_use_candidate,
clippy::missing_panics_doc,
clippy::identity_op
)]
use std::{
collections::{BTreeMap, BTreeSet, VecDeque},
fmt::{Debug, Display},
};
use nom::{
bytes::complete::tag,
character::complete::{newline, u32},
combinator::map,
multi::separated_list1,
sequence::{separated_pair, terminated, tuple},
IResult,
};
type SupportMap = BTreeMap<usize, BTreeSet<usize>>;
type Input = (SupportMap, SupportMap);
fn parse(input: &str) -> Input {
let mut idx = 0usize;
let triple = |s| {
map(
tuple((terminated(u32, tag(",")), terminated(u32, tag(",")), u32)),
|(x, y, z)| Triple::new(z, y, x),
)(s)
};
let result: IResult<&str, Input> = map(
separated_list1(
newline,
map(separated_pair(triple, tag("~"), triple), |(start, end)| {
idx += 1;
Brick::new(idx, start, end)
}),
),
|x| settle(&x),
)(input);
result.unwrap().1
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
struct Triple {
z: u32,
y: u32,
x: u32,
}
impl Triple {
fn new(z: u32, y: u32, x: u32) -> Self {
Self { z, y, x }
}
fn below(&self) -> Self {
Self {
z: self.z - 1,
y: self.y,
x: self.x,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
struct Brick {
id: usize,
min: Triple,
max: Triple,
cubes: BTreeSet<Triple>,
}
impl Brick {
fn new(id: usize, start: Triple, end: Triple) -> Self {
let min = start.min(end);
let max = start.max(end);
let cubes = Self::cubes(min, max);
Self {
id,
min,
max,
cubes,
}
}
fn on_ground(&self) -> bool {
self.min.z == 1
}
fn move_down(&mut self) {
self.min.z -= 1;
self.max.z -= 1;
self.cubes = Self::cubes(self.min, self.max);
}
fn cubes(min: Triple, max: Triple) -> BTreeSet<Triple> {
(min.z..=max.z)
.flat_map(|z| {
(min.y..=max.y)
.flat_map(move |y| (min.x..=max.x).map(move |x| Triple::new(z, y, x)))
})
.collect()
}
}
impl Display for Brick {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}: {},{},{}~{},{},{}",
self.id, self.min.z, self.min.y, self.min.x, self.max.z, self.max.y, self.max.x
)
}
}
impl PartialOrd for Brick {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Brick {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.min.cmp(&other.min).then(self.max.cmp(&other.max))
}
}
fn settle(input: &[Brick]) -> Input {
let mut bricks = input.to_vec();
bricks.sort();
let mut settled: BTreeMap<Triple, usize> = BTreeMap::new();
let mut holding_up: SupportMap = BTreeMap::new();
let mut sitting_on: SupportMap = BTreeMap::new();
for brick in bricks.iter_mut() {
holding_up.insert(brick.id, BTreeSet::new());
sitting_on.insert(brick.id, BTreeSet::new());
let bricks_below = loop {
if brick.on_ground() {
break vec![];
}
let bricks_below: Vec<usize> = brick
.cubes
.iter()
.filter_map(|c| settled.get(&c.below()))
.cloned()
.collect();
if !bricks_below.is_empty() {
break bricks_below;
}
brick.move_down();
};
settled.extend(brick.cubes.iter().map(|c| (*c, brick.id)));
for below in bricks_below {
holding_up.entry(below).or_default().insert(brick.id);
sitting_on.entry(brick.id).or_default().insert(below);
}
}
(holding_up, sitting_on)
}
fn part1(input: &Input) -> usize {
let (holding_up, sitting_on) = input;
holding_up
.iter()
.filter(|(_brick, above)| above.iter().all(|a| sitting_on[&a].len() != 1))
.count()
}
fn part2(input: &Input) -> usize {
let (holding_up, sitting_on) = input;
holding_up
.iter()
.map(|(brick, above)| {
let mut queue: VecDeque<usize> = VecDeque::from_iter(above.iter().cloned());
let mut unsupported: BTreeSet<usize> = BTreeSet::from_iter(vec![*brick]);
while let Some(brick) = queue.pop_front() {
if sitting_on[&brick].is_subset(&unsupported) {
unsupported.insert(brick);
for b in &holding_up[&brick] {
queue.push_back(*b);
}
}
}
unsupported.len() - 1
})
.sum()
}
pub fn main() {
let input = include_str!("../../../input/22.txt");
let input = parse(input);
println!("Part 1 : {}", part1(&input));
println!("Part 2 : {}", part2(&input));
}