/
17.rs
144 lines (132 loc) · 4.73 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
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
use std::collections::BTreeSet;
mod utils;
use utils::unique::*;
#[derive(Ord, PartialOrd, PartialEq, Eq, Clone, Debug)]
struct Position {
x: i64,
y: i64,
z: i64,
w: i64,
}
impl Position {
fn new(x: i64, y: i64, z: i64, w: i64) -> Self {
Position { x, y, z, w }
}
fn get_block(&self, is_4_dimensional: bool) -> Vec<Self> {
(-1..=1)
.map(|x_offset| {
(-1..=1)
.map(|y_offset| {
(-1..=1)
.map(|z_offset| {
(-1 * is_4_dimensional as i64..=is_4_dimensional as i64)
.map(|w_offset| {
Position::new(
self.x + x_offset,
self.y + y_offset,
self.z + z_offset,
self.w + w_offset,
)
})
.collect::<Vec<Self>>()
})
.flatten()
.collect::<Vec<Self>>()
})
.flatten()
.collect::<Vec<Self>>()
})
.flatten()
.collect()
}
fn get_neighbors(&self, active_cubes: &BTreeSet<Self>, is_4_dimensional: bool) -> u32 {
(-1..=1)
.map(|x_offset| {
(-1..=1)
.map(|y_offset| {
(-1..=1)
.map(|z_offset| {
(-1 * is_4_dimensional as i64..=is_4_dimensional as i64)
.filter(|w_offset| {
x_offset != 0
|| y_offset != 0
|| z_offset != 0
|| *w_offset != 0
})
.map(|w_offset| {
Position::new(
self.x + x_offset,
self.y + y_offset,
self.z + z_offset,
self.w + w_offset,
)
})
.filter(|neighbor| active_cubes.contains(neighbor))
.count() as u32
})
.sum::<u32>()
})
.sum::<u32>()
})
.sum()
}
}
fn parse_map(input: &str) -> BTreeSet<Position> {
input
.lines()
.rev()
.enumerate()
.map(|(y_index, line)| {
line.chars()
.enumerate()
.filter(|(_x_index, character)| *character == '#')
.map(|(x_index, _char)| Position::new(x_index as i64, y_index as i64, 0, 0))
.collect::<BTreeSet<Position>>()
})
.flatten()
.collect::<BTreeSet<Position>>()
}
fn cycle(active_cubes: &BTreeSet<Position>, is_4_dimensional: bool) -> BTreeSet<Position> {
active_cubes
.iter()
.map(|current_active| current_active.get_block(is_4_dimensional))
.flatten()
.unique()
.filter(|possible_cube| {
let is_active = active_cubes.contains(possible_cube);
let neighbors = possible_cube.get_neighbors(active_cubes, is_4_dimensional);
matches!((is_active, neighbors), (true, 2..=3) | (false, 3))
})
.collect()
}
fn cycles(
active_cubes: &BTreeSet<Position>,
nth: usize,
is_4_dimensional: bool,
) -> BTreeSet<Position> {
let mut current_active = cycle(active_cubes, is_4_dimensional);
for _ in 1..nth {
current_active = cycle(¤t_active, is_4_dimensional);
}
current_active
}
fn solve_part_one(active_cubes: &BTreeSet<Position>) {
let cubes = cycles(active_cubes, 6, false).len();
println!(
"There are {} cubes left in the active state after the sixth cycle.",
cubes
);
}
fn solve_part_two(active_cubes: &BTreeSet<Position>) {
let cubes = cycles(active_cubes, 6, true).len();
println!(
"There are {} cubes left in the active state after the sixth cycle in 4 dimensions.",
cubes
);
}
fn main() {
let input = include_str!("17_data.map");
let active_cubes = parse_map(input);
solve_part_one(&active_cubes);
solve_part_two(&active_cubes);
}