1use self::implementation::*;
20use crate::util::hash::*;
21
22#[derive(Eq, Hash, PartialEq)]
23pub struct Hex {
24 q: i32,
25 r: i32,
26}
27
28pub fn parse(input: &str) -> FastSet<Hex> {
29 let mut tiles = FastSet::new();
30
31 for line in input.lines() {
32 let mut iter = line.bytes();
33 let mut q = 0;
34 let mut r = 0;
35
36 while let Some(b) = iter.next() {
37 match b {
38 b'e' => q += 1,
39 b'w' => q -= 1,
40 b'n' => {
41 r -= 1;
42 q += i32::from(iter.next().unwrap() == b'e');
43 }
44 b's' => {
45 r += 1;
46 q -= i32::from(iter.next().unwrap() == b'w');
47 }
48 _ => unreachable!(),
49 }
50 }
51
52 let tile = Hex { q, r };
53 if !tiles.remove(&tile) {
54 tiles.insert(tile);
55 }
56 }
57
58 tiles
59}
60
61pub fn part1(input: &FastSet<Hex>) -> usize {
62 input.len()
63}
64
65pub fn part2(input: &FastSet<Hex>) -> usize {
66 simulate(input)
67}
68
69#[cfg(not(feature = "simd"))]
70mod implementation {
71 use super::*;
72
73 pub(super) fn simulate(input: &FastSet<Hex>) -> usize {
74 let (q1, q2, r1, r2) =
76 input.iter().fold((i32::MAX, i32::MIN, i32::MAX, i32::MIN), |(q1, q2, r1, r2), hex| {
77 (q1.min(hex.q), q2.max(hex.q), r1.min(hex.r), r2.max(hex.r))
78 });
79
80 let width = (q2 - q1 + 203) as usize;
83 let height = (r2 - r1 + 203) as usize;
84
85 let mut state = vec![0_u8; width * height];
86 let mut active = Vec::with_capacity(5_000);
87 let mut candidates = Vec::with_capacity(5_000);
88
89 for hex in input {
91 let index = width * (hex.r - r1 + 101) as usize + (hex.q - q1 + 101) as usize;
92 active.push(index);
93 }
94
95 for _ in 0..100 {
96 state.fill(0);
97
98 for &tile in &active {
99 for index in [
100 tile + 1,
101 tile - 1,
102 tile + width,
103 tile - width,
104 tile + 1 - width,
105 tile - 1 + width,
106 ] {
107 state[index] += 1;
108
109 if state[index] == 2 {
110 candidates.push(index);
111 }
112 }
113 }
114
115 active.retain(|&tile| state[tile] == 1);
117 candidates.retain(|&tile| state[tile] == 2);
119 active.append(&mut candidates);
120 }
121
122 active.len()
123 }
124}
125
126#[cfg(feature = "simd")]
127mod implementation {
128 use std::simd::prelude::*;
129
130 use super::*;
131
132 const LANE_WIDTH: usize = 32;
133 type Vector = Simd<u8, LANE_WIDTH>;
134
135 pub(super) fn simulate(input: &FastSet<Hex>) -> usize {
136 let (q1, q2, r1, r2) =
138 input.iter().fold((i32::MAX, i32::MIN, i32::MAX, i32::MIN), |(q1, q2, r1, r2), hex| {
139 (q1.min(hex.q), q2.max(hex.q), r1.min(hex.r), r2.max(hex.r))
140 });
141
142 let width = 2 + ((q2 - q1 + 201) as usize).next_multiple_of(LANE_WIDTH);
145 let height = (r2 - r1 + 203) as usize;
146
147 let mut current = vec![0; width * height];
149 let mut next = vec![0; width * height];
150
151 for hex in input {
152 let index = width * (hex.r - r1 + 101) as usize + (hex.q - q1 + 101) as usize;
153 current[index] = 1;
154 }
155
156 let zero = Simd::splat(0);
157 let one = Simd::splat(1);
158 let two = Simd::splat(2);
159
160 for round in 0..100 {
161 let edge = 100 - round;
163
164 for x in (edge..width - edge).step_by(LANE_WIDTH) {
165 for y in edge..height - edge {
166 let index = width * y + x;
167 let tiles = Simd::from_slice(¤t[index..]);
168
169 let above = left_center(¤t, index - width);
170 let row = left_right(¤t, index);
171 let below = center_right(¤t, index + width);
172 let total = row + above + below;
173
174 let first = (tiles.simd_eq(one) & total.simd_eq(one)).select(one, zero);
176 let second = total.simd_eq(two).select(one, zero);
178
179 let result = first + second;
180 result.copy_to_slice(&mut next[index..]);
181 }
182 }
183
184 (current, next) = (next, current);
185 }
186
187 current.iter().map(|&b| usize::from(b)).sum()
188 }
189
190 #[inline]
191 fn left_center(current: &[u8], index: usize) -> Vector {
192 let center = Simd::from_slice(¤t[index..]);
193 let left = center.shift_elements_left::<1>(current[index + LANE_WIDTH]);
194 left + center
195 }
196
197 #[inline]
198 fn left_right(current: &[u8], index: usize) -> Vector {
199 let center = Simd::from_slice(¤t[index..]);
200 let left = center.shift_elements_left::<1>(current[index + LANE_WIDTH]);
201 let right = center.shift_elements_right::<1>(current[index - 1]);
202 left + right
203 }
204
205 #[inline]
206 fn center_right(current: &[u8], index: usize) -> Vector {
207 let center = Simd::from_slice(¤t[index..]);
208 let right = center.shift_elements_right::<1>(current[index - 1]);
209 center + right
210 }
211}