Skip to content

Commit 3c8d8b5

Browse files
committed
Add day 17
1 parent b21736f commit 3c8d8b5

File tree

1 file changed

+40
-31
lines changed

1 file changed

+40
-31
lines changed

2020/17/17.rs

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,57 @@
11
use std::{collections::HashSet, io::stdin};
22

3-
use itertools::Itertools;
3+
use itertools::{iproduct, Itertools};
44

5-
fn adjacent(xyz: (i32, i32, i32, i32)) -> HashSet<(i32, i32, i32, i32)> {
6-
(-1..=1)
7-
.cartesian_product(-1..=1)
8-
.cartesian_product(-1..=1)
9-
.cartesian_product(-1..=1)
10-
.map(|(((x, y), z), w)| (x + xyz.0, y + xyz.1, z + xyz.2, w+xyz.3))
11-
.collect()
12-
}
5+
fn adjacent(p: &Vec<i32>) -> HashSet<Vec<i32>> {
6+
let r = [-1, 0, 1];
137

14-
fn main() {
15-
let mut active = stdin()
16-
.lines()
17-
.enumerate()
18-
.map(|(y, line)| {
19-
line.unwrap()
20-
.chars()
21-
.enumerate()
22-
.filter(|(_, c)| *c == '#')
23-
.map(|(x, _)| (y as i32, x as i32, 0, 0))
24-
.collect_vec()
25-
})
26-
.flatten()
27-
.collect::<HashSet<_>>();
28-
println!("{:?}", active);
8+
match p.len() {
9+
3 => iproduct!(r, r, r)
10+
.map(|(dx, dy, dz)| vec![p[0] + dx, p[1] + dy, p[2] + dz])
11+
.collect(),
12+
4 => iproduct!(r, r, r, r)
13+
.map(|(dx, dy, dz, dw)| vec![p[0] + dx, p[1] + dy, p[2] + dz, p[3] + dw])
14+
.collect(),
15+
_ => HashSet::new(),
16+
}
17+
}
2918

30-
println!("{:?}", active.len());
19+
fn solve(initial_active: HashSet<Vec<i32>>) {
20+
let mut active = initial_active;
3121
for _ in 0..6 {
3222
let mut new_active = HashSet::new();
3323
let mut inactive = HashSet::new();
3424
for xyz in active.iter() {
35-
inactive.extend(adjacent(*xyz).difference(&active));
36-
if (3..=4).contains(&adjacent(*xyz).intersection(&active).count()) {
37-
new_active.insert(*xyz);
25+
let adj = adjacent(xyz);
26+
inactive.extend(adj.difference(&active).cloned());
27+
if (3..=4).contains(&adj.intersection(&active).count()) {
28+
new_active.insert(xyz.to_vec());
3829
}
3930
}
4031
for xyz in inactive.iter() {
41-
if adjacent(*xyz).intersection(&active).count() == 3 {
42-
new_active.insert(*xyz);
32+
if adjacent(xyz).intersection(&active).count() == 3 {
33+
new_active.insert(xyz.to_vec());
4334
}
4435
}
4536
active = new_active;
46-
println!("{:?}", active.len());
4737
}
38+
println!("{:?}", active.len());
4839
}
40+
41+
fn main() {
42+
let active = stdin()
43+
.lines()
44+
.enumerate()
45+
.map(|(y, line)| {
46+
line.unwrap()
47+
.chars()
48+
.enumerate()
49+
.filter(|(_, c)| *c == '#')
50+
.map(|(x, _)| vec![y as i32, x as i32, 0])
51+
.collect_vec()
52+
})
53+
.flatten()
54+
.collect::<HashSet<_>>();
55+
solve(active.clone());
56+
solve(active.iter().map(|v| v.iter().cloned().chain(vec![4]).collect()).collect());
57+
}

0 commit comments

Comments
 (0)