-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday22.rs
413 lines (340 loc) · 11.9 KB
/
day22.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
use crate::solutions::Solution;
use crate::utils::point3d::Point3D;
use itertools::Itertools;
use std::collections::{HashMap, HashSet, VecDeque};
use std::fmt::{Display, Formatter};
pub struct Day22;
impl Solution for Day22 {
fn part_one(&self, input: &str) -> String {
let bricks: Bricks = Self::parse_input(input);
let (supported_by, supporters) = Self::graphs(&bricks);
bricks
.bricks_by_lowest_z_asc()
.iter()
.filter(|brick| {
supported_by.get(brick).unwrap().iter().all(|brick_above| {
let supporters: Vec<&Brick> = supporters
.get(brick_above)
.unwrap()
.iter()
.filter(|b| brick != b)
.collect();
!supporters.is_empty()
})
})
.collect::<Vec<_>>()
.len()
.to_string()
}
fn part_two(&self, input: &str) -> String {
let bricks: Bricks = Self::parse_input(input);
let (supported_by, supporters) = Self::graphs(&bricks);
bricks
.bricks_by_lowest_z_asc()
.iter()
.map(|b| Self::fall(&supported_by, &supporters, b.clone()))
.sum::<isize>()
.to_string()
}
}
impl Day22 {
fn parse_input(input: &str) -> Bricks {
let bricks: Vec<Brick> = input.lines().map(Brick::from).collect();
Self::settle_down(bricks)
}
fn settle_down(bricks: Vec<Brick>) -> Bricks {
let mut settled_down: Vec<Brick> = Vec::with_capacity(bricks.len());
let bricks_from_down = bricks
.iter()
.sorted_by(|a, b| a.lowest_z().cmp(&b.lowest_z()));
for brick in bricks_from_down {
let mut brick = brick.clone();
let bricks_below = settled_down.iter().rev().take(50).collect::<Vec<&Brick>>();
loop {
let brick_below = brick.down();
if bricks_below.iter().any(|b| b.collide(&brick_below))
|| brick_below.lowest_z() == 0
{
settled_down.push(brick.clone());
break;
}
brick = brick_below;
}
}
Bricks::new(settled_down)
}
fn graphs(bricks: &Bricks) -> (HashMap<Brick, Vec<Brick>>, HashMap<Brick, Vec<Brick>>) {
// if remove brick (key) the another bricks will fall (values)
let supported_by: HashMap<Brick, Vec<Brick>> = bricks
.bricks_by_highest_z_desc()
.iter()
.map(|brick| {
let above = bricks
.in_z(brick.highest_z() + 1)
.into_iter()
.filter(|b| brick.up().collide(b))
.collect();
(brick.clone(), above)
})
.collect();
// is brick (key) supported by another bricks (values)
let mut supporters: HashMap<Brick, Vec<Brick>> = HashMap::new();
for (brick, above) in &supported_by {
for a in above {
supporters.entry(a.clone()).or_default().push(brick.clone())
}
}
(supported_by, supporters)
}
fn fall(
supported_by: &HashMap<Brick, Vec<Brick>>,
supporters: &HashMap<Brick, Vec<Brick>>,
brick: Brick,
) -> isize {
let mut to_remove: VecDeque<Brick> = VecDeque::new();
to_remove.push_back(brick);
let mut already_removed: Vec<Brick> = Vec::new();
let mut count: isize = 0;
while let Some(current) = to_remove.pop_front() {
let next_to_fall = supported_by.get(¤t).unwrap();
already_removed.push(current.clone());
for next in next_to_fall {
let supporters: Vec<&Brick> = supporters
.get(next)
.unwrap()
.iter()
.filter(|b| !already_removed.contains(b))
.collect();
if supporters.is_empty() {
to_remove.push_back(next.clone());
if !already_removed.contains(next) {
count += 1;
}
}
}
}
count
}
}
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
struct Brick {
from: Point3D,
to: Point3D,
points: Vec<Point3D>,
}
impl Brick {
fn new(from: Point3D, to: Point3D) -> Self {
let mut points: Vec<Point3D> = Vec::new();
for x in from.x.min(to.x)..=from.x.max(to.x) {
for y in from.y.min(to.y)..=from.y.max(to.y) {
for z in from.z.min(to.z)..=from.z.max(to.z) {
points.push(Point3D::new(x, y, z));
}
}
}
Self { from, to, points }
}
fn lowest_z(&self) -> isize {
self.from.z.min(self.to.z)
}
fn highest_z(&self) -> isize {
self.from.z.max(self.to.z)
}
#[cfg(test)]
fn len(&self) -> usize {
self.points.len()
}
fn down(&self) -> Self {
Self::new(self.from.down(), self.to.down())
}
fn up(&self) -> Self {
Self::new(self.from.up(), self.to.up())
}
fn collide(&self, other: &Self) -> bool {
self.points.iter().any(|p| other.points.contains(p))
}
}
impl From<&str> for Brick {
fn from(value: &str) -> Self {
let (left, right) = value.split_terminator('~').collect_tuple().unwrap();
Self::new(Point3D::from(left), Point3D::from(right))
}
}
impl Display for Brick {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let Point3D {
x: fx,
y: fy,
z: fz,
} = self.from;
let Point3D {
x: tx,
y: ty,
z: tz,
} = self.to;
write!(f, "{fx},{fy},{fz}~{tx},{ty},{tz}")
}
}
struct Bricks {
bricks: Vec<Brick>,
bricks_in_row: HashMap<isize, HashSet<Brick>>,
}
impl Bricks {
fn new(bricks: Vec<Brick>) -> Self {
let mut bricks_in_row: HashMap<isize, HashSet<Brick>> = HashMap::new();
for brick in &bricks {
for point in &brick.points {
bricks_in_row
.entry(point.z)
.or_default()
.insert(brick.clone());
}
}
Self {
bricks,
bricks_in_row,
}
}
fn in_z(&self, z: isize) -> HashSet<Brick> {
self.bricks_in_row
.get(&z)
.unwrap_or(&HashSet::new())
.clone()
}
fn bricks_by_lowest_z_asc(&self) -> Vec<Brick> {
self.bricks
.clone()
.into_iter()
.sorted_by(|a, b| a.lowest_z().cmp(&b.lowest_z()))
.collect()
}
fn bricks_by_highest_z_desc(&self) -> Vec<Brick> {
self.bricks
.clone()
.into_iter()
.sorted_by(|a, b| a.highest_z().cmp(&b.highest_z()).reverse())
.collect()
}
#[cfg(test)]
fn push_brick(&self, brick: Brick) -> Self {
let mut new_bricks = self.bricks.clone();
new_bricks.push(brick.clone());
Bricks::new(new_bricks)
}
}
#[cfg(test)]
mod tests {
use crate::solutions::year2023::day22::{Brick, Bricks, Day22};
use crate::solutions::year2023::read_2023_example;
use crate::solutions::Solution;
#[test]
fn part_one_example_test() {
let input = read_2023_example("22");
assert_eq!("5", Day22.part_one(input.as_str()));
}
#[test]
fn part_two_example_test() {
let input = read_2023_example("22");
assert_eq!("7", Day22.part_two(input.as_str()));
}
#[test]
fn brick_len_test() {
assert_eq!(1, Brick::from("2,2,2~2,2,2").len());
assert_eq!(2, Brick::from("0,0,10~1,0,10").len());
assert_eq!(2, Brick::from("0,0,10~0,1,10").len());
assert_eq!(10, Brick::from("0,0,1~0,0,10").len());
}
#[test]
fn fall() {
// a is on the ground
let a = Brick::from("1,0,0~1,2,0");
// b and c is on a
let b = Brick::from("0,0,1~2,0,1");
assert!(b.down().collide(&a));
let c = Brick::from("0,2,1~2,2,1");
assert!(c.down().collide(&a));
// d is on b
let d = Brick::from("0,0,2~2,0,2");
assert!(d.down().collide(&b));
// e is on c
let e = Brick::from("0,2,2~2,2,2");
assert!(e.down().collide(&c));
let bricks = Bricks::new(vec![a.clone(), b.clone(), c.clone(), d.clone(), e.clone()]);
let (g1, g2) = Day22::graphs(&bricks);
assert_eq!(4, Day22::fall(&g1, &g2, a.clone()));
assert_eq!(1, Day22::fall(&g1, &g2, b.clone()));
assert_eq!(1, Day22::fall(&g1, &g2, c.clone()));
assert_eq!(0, Day22::fall(&g1, &g2, d.clone()));
assert_eq!(0, Day22::fall(&g1, &g2, e.clone()));
// ------------
// horz is on d and e
let horz = Brick::from("1,0,3~1,2,3");
assert!(horz.down().collide(&d));
assert!(horz.down().collide(&e));
let bricks = bricks.push_brick(horz.clone());
let (g1, g2) = Day22::graphs(&bricks);
assert_eq!(1, Day22::fall(&g1, &g2, b.clone()));
assert_eq!(1, Day22::fall(&g1, &g2, c.clone()));
assert_eq!(0, Day22::fall(&g1, &g2, d.clone()));
assert_eq!(0, Day22::fall(&g1, &g2, e.clone()));
assert_eq!(0, Day22::fall(&g1, &g2, horz.clone()));
assert_eq!(5, Day22::fall(&g1, &g2, a.clone()));
// ------------
// horz2 is on horz
let horz2 = Brick::from("1,0,4~1,2,4");
assert!(horz2.down().collide(&horz));
let bricks = bricks.push_brick(horz2.clone());
let (g1, g2) = Day22::graphs(&bricks);
assert_eq!(1, Day22::fall(&g1, &g2, b.clone()));
assert_eq!(1, Day22::fall(&g1, &g2, c.clone()));
assert_eq!(0, Day22::fall(&g1, &g2, d.clone()));
assert_eq!(0, Day22::fall(&g1, &g2, e.clone()));
assert_eq!(1, Day22::fall(&g1, &g2, horz.clone()));
assert_eq!(0, Day22::fall(&g1, &g2, horz2.clone()));
assert_eq!(6, Day22::fall(&g1, &g2, a.clone()));
}
#[test]
fn fall_edge_case_with_two_cubes() {
let horz2 = Brick::from("1,0,1~1,2,1");
// stick is on horz
let stick = Brick::from("1,0,2~1,0,3");
assert!(stick.down().collide(&horz2));
// cubes are on horz2 too
let cube1 = Brick::from("1,2,2~1,2,2");
let cube2 = Brick::from("1,2,3~1,2,3");
assert!(cube1.down().collide(&horz2));
assert!(cube2.down().collide(&cube1));
assert!(!cube1.collide(&stick));
assert!(!cube2.collide(&stick));
// horz3 is on cube2 and stick
let horz3 = Brick::from("1,0,4~1,2,4");
assert!(horz3.down().collide(&cube2));
assert!(horz3.down().collide(&stick));
let bricks = Bricks::new(vec![
stick.clone(),
cube1.clone(),
cube2.clone(),
horz3.clone(),
horz2.clone(),
]);
let (g1, g2) = Day22::graphs(&bricks);
assert_eq!(4, Day22::fall(&g1, &g2, horz2));
assert_eq!(1, Day22::fall(&g1, &g2, cube1));
assert_eq!(0, Day22::fall(&g1, &g2, cube2));
assert_eq!(0, Day22::fall(&g1, &g2, stick));
}
// https://www.reddit.com/r/adventofcode/comments/18p4wlj/comment/kemjie2/
#[test]
fn fall_reddit_comment() {
let a = Brick::from("0,0,1~0,5,1");
let b = Brick::from("0,6,1~0,9,1");
let c = Brick::from("0,0,2~0,0,2");
let d = Brick::from("0,3,2~0,8,2");
let bricks = Bricks::new(vec![a.clone(), b.clone(), c.clone(), d.clone()]);
let (g1, g2) = Day22::graphs(&bricks);
assert_eq!(1, Day22::fall(&g1, &g2, a));
assert_eq!(0, Day22::fall(&g1, &g2, b));
assert_eq!(0, Day22::fall(&g1, &g2, c));
assert_eq!(0, Day22::fall(&g1, &g2, d));
}
}