-
Notifications
You must be signed in to change notification settings - Fork 7
/
17.rs
57 lines (52 loc) · 1.66 KB
/
17.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
use std::{collections::HashSet, io::stdin};
use itertools::{iproduct, Itertools};
fn adjacent(p: &Vec<i32>) -> HashSet<Vec<i32>> {
let r = [-1, 0, 1];
match p.len() {
3 => iproduct!(r, r, r)
.map(|(dx, dy, dz)| vec![p[0] + dx, p[1] + dy, p[2] + dz])
.collect(),
4 => iproduct!(r, r, r, r)
.map(|(dx, dy, dz, dw)| vec![p[0] + dx, p[1] + dy, p[2] + dz, p[3] + dw])
.collect(),
_ => HashSet::new(),
}
}
fn solve(initial_active: HashSet<Vec<i32>>) {
let mut active = initial_active;
for _ in 0..6 {
let mut new_active = HashSet::new();
let mut inactive = HashSet::new();
for xyz in active.iter() {
let adj = adjacent(xyz);
inactive.extend(adj.difference(&active).cloned());
if (3..=4).contains(&adj.intersection(&active).count()) {
new_active.insert(xyz.to_vec());
}
}
for xyz in inactive.iter() {
if adjacent(xyz).intersection(&active).count() == 3 {
new_active.insert(xyz.to_vec());
}
}
active = new_active;
}
println!("{:?}", active.len());
}
fn main() {
let active = stdin()
.lines()
.enumerate()
.map(|(y, line)| {
line.unwrap()
.chars()
.enumerate()
.filter(|(_, c)| *c == '#')
.map(|(x, _)| vec![y as i32, x as i32, 0])
.collect_vec()
})
.flatten()
.collect::<HashSet<_>>();
solve(active.clone());
solve(active.iter().map(|v| v.iter().cloned().chain(vec![4]).collect()).collect());
}