1use self::implementation::*;
15use crate::util::grid::*;
16
17pub fn parse(input: &str) -> Grid<u8> {
21 Grid::parse(input)
22}
23
24pub fn part1(input: &Grid<u8>) -> usize {
26 three_dimensions(input)
27}
28
29pub fn part2(input: &Grid<u8>) -> usize {
31 four_dimensions(input)
32}
33
34#[cfg(not(feature = "simd"))]
35mod implementation {
36 use super::*;
37 use crate::util::point::*;
38
39 const X: i32 = 22;
44 const Y: i32 = 22;
45 const Z: i32 = 15;
46 const W: i32 = 15;
47
48 const STRIDE_X: i32 = 1;
51 const STRIDE_Y: i32 = X * STRIDE_X;
52 const STRIDE_Z: i32 = Y * STRIDE_Y;
53 const STRIDE_W: i32 = Z * STRIDE_Z;
54
55 pub(super) fn three_dimensions(input: &Grid<u8>) -> usize {
56 let size = X * Y * Z;
57 let base = STRIDE_X + STRIDE_Y + STRIDE_Z;
58 boot_process(input, size, base, &[0])
59 }
60
61 pub(super) fn four_dimensions(input: &Grid<u8>) -> usize {
62 let size = X * Y * Z * W;
63 let base = STRIDE_X + STRIDE_Y + STRIDE_Z + STRIDE_W;
64 boot_process(input, size, base, &[-1, 0, 1])
65 }
66
67 fn boot_process(input: &Grid<u8>, size: i32, base: i32, fourth_dimension: &[i32]) -> usize {
69 let dimension = [-1, 0, 1];
70 let mut neighbors = Vec::new();
71
72 for x in dimension {
74 for y in dimension {
75 for z in dimension {
76 for w in fourth_dimension {
77 let offset = x * STRIDE_X + y * STRIDE_Y + z * STRIDE_Z + w * STRIDE_W;
78 if offset != 0 {
79 neighbors.push(offset as isize);
80 }
81 }
82 }
83 }
84 }
85
86 let mut active = Vec::with_capacity(5_000);
87 let mut candidates = Vec::with_capacity(5_000);
88
89 for x in 0..input.width {
93 for y in 0..input.height {
94 if input[Point::new(x, y)] == b'#' {
95 let index = 7 * base + x + y * STRIDE_Y;
96 active.push(index as usize);
97 }
98 }
99 }
100
101 for _ in 0..6 {
102 let mut state = vec![0_u8; size as usize];
103
104 for &cube in &active {
105 for &offset in &neighbors {
106 let index = cube.wrapping_add_signed(offset);
110 state[index] += 1;
111
112 if state[index] == 3 {
113 candidates.push(index);
114 }
115 }
116 }
117
118 active.retain(|&cube| state[cube] == 2);
120 candidates.retain(|&cube| state[cube] == 3);
122 active.append(&mut candidates);
123 }
124
125 active.len()
126 }
127}
128
129#[cfg(feature = "simd")]
130mod implementation {
131 use std::simd::prelude::*;
132
133 use super::*;
134
135 const LANE_WIDTH: usize = 32;
136 type Vector = Simd<u8, LANE_WIDTH>;
137
138 pub(super) fn three_dimensions(input: &Grid<u8>) -> usize {
139 let mut current = vec![[[0; 32]; 22]; 15];
143 let mut next = current.clone();
144 let mut first = current.clone();
146
147 for point in input.points() {
149 if input[point] == b'#' {
150 current[7][point.y as usize + 7][point.x as usize + 7] = 1;
151 }
152 }
153
154 let zero = Simd::splat(0);
155 let one = Simd::splat(1);
156 let three = Simd::splat(3);
157
158 for round in 0..6 {
159 let edge = 5 - round;
161
162 for z in (1 + edge)..(14 - edge) {
164 for y in (1 + edge)..(21 - edge) {
165 let above = xs_sum(¤t[z][y - 1]);
166 let row = xs_sum(¤t[z][y]);
167 let below = xs_sum(¤t[z][y + 1]);
168
169 (row + above + below).copy_to_slice(&mut first[z][y]);
170 }
171 }
172
173 for z in (1 + edge)..(14 - edge) {
175 for y in (1 + edge)..(21 - edge) {
176 let above = from_slice(&first[z - 1][y]);
177 let row = from_slice(&first[z][y]);
178 let below = from_slice(&first[z + 1][y]);
179 let state = from_slice(¤t[z][y]);
180
181 let total = row + above + below - state;
182 let result = (state | total).simd_eq(three).select(one, zero);
183 result.copy_to_slice(&mut next[z][y]);
184 }
185 }
186
187 (current, next) = (next, current);
188 }
189
190 current.iter().flatten().flatten().map(|&b| usize::from(b)).sum()
191 }
192
193 pub(super) fn four_dimensions(input: &Grid<u8>) -> usize {
194 let mut current = vec![[[[0; 32]; 22]; 15]; 15];
196 let mut next = current.clone();
197 let mut first = current.clone();
199 let mut second = current.clone();
200
201 for point in input.points() {
203 if input[point] == b'#' {
204 current[7][7][point.y as usize + 7][point.x as usize + 7] = 1;
205 }
206 }
207
208 let zero = Simd::splat(0);
209 let one = Simd::splat(1);
210 let three = Simd::splat(3);
211
212 for round in 0..6 {
213 let edge = 5 - round;
215
216 for w in (1 + edge)..(14 - edge) {
218 for z in (1 + edge)..(14 - edge) {
219 for y in (1 + edge)..(21 - edge) {
220 let above = xs_sum(¤t[w][z][y - 1]);
221 let row = xs_sum(¤t[w][z][y]);
222 let below = xs_sum(¤t[w][z][y + 1]);
223
224 (row + above + below).copy_to_slice(&mut first[w][z][y]);
225 }
226 }
227 }
228
229 for w in (1 + edge)..(14 - edge) {
231 for z in (1 + edge)..(14 - edge) {
232 for y in (1 + edge)..(21 - edge) {
233 let above = from_slice(&first[w][z - 1][y]);
234 let row = from_slice(&first[w][z][y]);
235 let below = from_slice(&first[w][z + 1][y]);
236
237 (row + above + below).copy_to_slice(&mut second[w][z][y]);
238 }
239 }
240 }
241
242 for w in (1 + edge)..(14 - edge) {
244 for z in (1 + edge)..(14 - edge) {
245 for y in (1 + edge)..(21 - edge) {
246 let above = from_slice(&second[w - 1][z][y]);
247 let row = from_slice(&second[w][z][y]);
248 let below = from_slice(&second[w + 1][z][y]);
249 let state = from_slice(¤t[w][z][y]);
250
251 let total = row + above + below - state;
252 let result = (state | total).simd_eq(three).select(one, zero);
253 result.copy_to_slice(&mut next[w][z][y]);
254 }
255 }
256 }
257
258 (current, next) = (next, current);
259 }
260
261 current.iter().flatten().flatten().flatten().map(|&b| usize::from(b)).sum()
262 }
263
264 #[inline]
265 fn from_slice(slice: &[u8]) -> Vector {
266 Simd::from_slice(slice)
267 }
268
269 #[inline]
271 fn xs_sum(slice: &[u8]) -> Vector {
272 let center = Simd::from_slice(slice);
273 let left = center.shift_elements_left::<1>(0);
274 let right = center.shift_elements_right::<1>(0);
275
276 center + left + right
277 }
278}