-
Notifications
You must be signed in to change notification settings - Fork 0
/
22.rs
138 lines (124 loc) · 3.55 KB
/
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
126
127
128
129
130
131
132
133
134
135
136
137
138
use std::collections::{HashMap, HashSet};
static INPUT_TXT: &str = include_str!("../../input/22.txt");
fn main() {
println!("Part 1: {}", part_1(INPUT_TXT));
println!("Part 2: {}", part_2(INPUT_TXT));
}
#[derive(Debug, Clone, Copy)]
struct Pos {
x: usize,
y: usize,
z: usize,
}
impl From<&str> for Pos {
fn from(s: &str) -> Self {
let mut split = s.split(',');
Self {
x: split.next().unwrap().parse().unwrap(),
y: split.next().unwrap().parse().unwrap(),
z: split.next().unwrap().parse().unwrap(),
}
}
}
#[derive(Debug, Clone, Copy)]
struct Brick {
from: Pos,
to: Pos,
}
impl From<&str> for Brick {
fn from(s: &str) -> Self {
let (from, to) = s.split_once('~').unwrap();
Self {
from: from.into(),
to: to.into(),
}
}
}
impl Brick {
fn intersects(&self, b: &Brick) -> bool {
((self.from.x..=self.to.x).contains(&b.from.x)
|| (self.from.x..=self.to.x).contains(&b.to.x)
|| (b.from.x..=b.to.x).contains(&self.from.x)
|| (b.from.x..=b.to.x).contains(&self.to.x))
&& ((self.from.y..=self.to.y).contains(&b.from.y)
|| (self.from.y..=self.to.y).contains(&b.to.y)
|| (b.from.y..=b.to.y).contains(&self.from.y)
|| (b.from.y..=b.to.y).contains(&self.to.y))
}
}
fn parse_input(input: &str) -> Vec<Brick> {
input.trim().lines().map(Brick::from).collect()
}
fn check_stability(
bricks: &mut [Brick],
mut supported_by: Option<&mut HashMap<usize, Vec<usize>>>,
) -> usize {
bricks.sort_unstable_by_key(|b| b.from.z);
let mut in_place: HashMap<usize, Vec<usize>> = HashMap::new();
let mut count = HashSet::new();
for i in 0..bricks.len() {
while bricks[i].from.z > 1 {
let mut falling = true;
let brick = &bricks[i];
for &j in in_place.get(&(brick.from.z - 1)).unwrap_or(&vec![]) {
let b = &bricks[j];
if brick.intersects(b) {
if let Some(sb) = &mut supported_by {
sb.entry(i).or_default().push(j);
}
falling = false;
}
}
if falling {
let brick = &mut bricks[i];
brick.from.z -= 1;
brick.to.z -= 1;
count.insert(i);
} else {
break;
}
}
in_place.entry(bricks[i].to.z).or_default().push(i);
}
count.len()
}
fn part_1(input: &str) -> usize {
let mut bricks = parse_input(input);
let mut supported_by = HashMap::new();
check_stability(&mut bricks, Some(&mut supported_by));
(0..bricks.len())
.filter(|i| !supported_by.values().any(|v| v.len() == 1 && v.contains(i)))
.count()
}
fn part_2(input: &str) -> usize {
let mut bricks = parse_input(input);
check_stability(&mut bricks, None);
(0..bricks.len())
.map(|i| {
let mut bricks = bricks.clone();
bricks.remove(i);
check_stability(&mut bricks, None)
})
.sum()
}
#[cfg(test)]
mod tests {
use super::*;
static INPUT: &str = "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 test_part_1() {
assert_eq!(part_1(INPUT), 5);
assert_eq!(part_1(INPUT_TXT), 421);
}
#[test]
fn test_part_2() {
assert_eq!(part_2(INPUT), 7);
assert_eq!(part_2(INPUT_TXT), 39247);
}
}