-
Notifications
You must be signed in to change notification settings - Fork 0
/
18.rs
184 lines (168 loc) · 4.67 KB
/
18.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
use std::{
collections::{HashSet, VecDeque},
str::FromStr,
};
static INPUT_TXT: &str = include_str!("../../input/18.txt");
fn main() {
println!("Part 1: {}", part_1(INPUT_TXT));
println!("Part 2: {}", part_2(INPUT_TXT));
}
type Point = (i64, i64, i64);
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Cube {
position: Point,
sides: [Point; 6],
}
impl Cube {
#[must_use]
pub fn new(position: Point) -> Self {
let x = position.0 * 2;
let y = position.1 * 2;
let z = position.2 * 2;
Self {
position,
sides: Self::sides((x, y, z)),
}
}
#[must_use]
pub fn sides((x, y, z): Point) -> [Point; 6] {
[
(x - 1, y, z),
(x + 1, y, z),
(x, y - 1, z),
(x, y + 1, z),
(x, y, z - 1),
(x, y, z + 1),
]
}
}
impl FromStr for Cube {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut nums = s.split(',').map(|s| s.parse::<i64>().unwrap());
let position = (
nums.next().unwrap(),
nums.next().unwrap(),
nums.next().unwrap(),
);
Ok(Self::new(position))
}
}
fn part_1(input: &str) -> usize {
let sides = input
.trim()
.lines()
.map(|l| l.parse::<Cube>().unwrap())
.collect::<HashSet<_>>()
.into_iter()
.flat_map(|c| c.sides.into_iter().collect::<Vec<_>>())
.collect::<Vec<_>>();
sides.len() - (sides.len() - sides.iter().copied().collect::<HashSet<_>>().len()) * 2
}
fn part_2(input: &str) -> i64 {
let cubes = input
.trim()
.lines()
.map(|l| l.parse::<Cube>().unwrap())
.collect::<HashSet<_>>();
let (x, y, z) = cubes.iter().fold(
(
(i64::MAX, i64::MIN),
(i64::MAX, i64::MIN),
(i64::MAX, i64::MIN),
),
|(mut x, mut y, mut z), cube| {
x.0 = x.0.min(cube.position.0);
x.1 = x.1.max(cube.position.0);
y.0 = y.0.min(cube.position.1);
y.1 = y.1.max(cube.position.1);
z.0 = z.0.min(cube.position.2);
z.1 = z.1.max(cube.position.2);
(x, y, z)
},
);
let wet_sides = {
let x_range = x.0 - 1..=x.1 + 1;
let y_range = y.0 - 1..=y.1 + 1;
let z_range = z.0 - 1..=z.1 + 1;
let cubes_pos = cubes.iter().map(|c| c.position).collect::<HashSet<_>>();
let mut water = HashSet::new();
let mut queue =
VecDeque::from(vec![(*x_range.start(), *y_range.start(), *z_range.start())]);
loop {
if let Some(position @ (x, y, z)) = queue.pop_front() {
if x_range.contains(&x)
&& y_range.contains(&y)
&& z_range.contains(&z)
&& !water.contains(&position)
&& !cubes_pos.contains(&position)
{
water.insert(position);
queue.extend(Cube::sides(position));
}
} else {
break water;
}
}
}
.into_iter()
.map(Cube::new)
.collect::<HashSet<_>>()
.iter()
.flat_map(|c| c.sides)
.collect::<HashSet<_>>();
let sides = cubes.iter().flat_map(|c| c.sides).collect::<Vec<_>>();
cubes
.iter()
.filter(|cube| cube.sides.iter().any(|s| wet_sides.contains(s)))
.fold((0, HashSet::new()), |(n, mut shared), cube| {
(
cube.sides
.iter()
.filter(|side| wet_sides.contains(side))
.fold(n, |n, side| {
if shared.contains(side) {
n
} else {
match sides.iter().filter(|s| s == &side).count() {
1 => n + 1,
2 => {
shared.insert(*side);
n + 1
}
_ => n,
}
}
}),
shared,
)
})
.0
}
#[cfg(test)]
mod day_18_tests {
use super::*;
static INPUT: &str = "2,2,2
1,2,2
3,2,2
2,1,2
2,3,2
2,2,1
2,2,3
2,2,4
2,2,6
1,2,5
3,2,5
2,1,5
2,3,5";
#[test]
fn test_part_1() {
assert_eq!(part_1(INPUT), 64);
assert_eq!(part_1(INPUT_TXT), 4308);
}
#[test]
fn test_part_2() {
assert_eq!(part_2(INPUT), 58);
assert_eq!(part_2(INPUT_TXT), 2540);
}
}