1use self::implementation::*;
24use crate::util::parse::*;
25use crate::util::thread::*;
26
27type Input = (u64, u16);
28type Result = (u64, Vec<u16>);
29
30pub fn parse(input: &str) -> Input {
31 let result = parallel(input);
32
33 let mut part_one = 0;
35 let mut part_two = vec![0; 130_321];
36
37 for (first, second) in result {
38 part_one += first;
39 part_two.iter_mut().zip(second).for_each(|(a, b)| *a += b);
40 }
41
42 (part_one, part_two.into_iter().max().unwrap())
43}
44
45pub fn part1(input: &Input) -> u64 {
46 input.0
47}
48
49pub fn part2(input: &Input) -> u16 {
50 input.1
51}
52
53#[cfg(not(feature = "simd"))]
54mod implementation {
55 use super::*;
56
57 pub(super) fn parallel(input: &str) -> Vec<Result> {
59 let numbers: Vec<_> = input.iter_unsigned().collect();
60 spawn_parallel_iterator(&numbers, worker)
61 }
62
63 fn worker(iter: ParIter<'_, u32>) -> Result {
64 let mut part_one = 0;
65 let mut part_two = vec![0; 130_321];
66 let mut seen = vec![u16::MAX; 130_321];
67
68 for (&zeroth, id) in iter.zip(0..) {
69 let first = hash(zeroth);
70 let second = hash(first);
71 let third = hash(second);
72
73 let mut a;
74 let mut b = to_index(zeroth, first);
75 let mut c = to_index(first, second);
76 let mut d = to_index(second, third);
77
78 let mut number = third;
79 let mut previous = third % 10;
80
81 for _ in 3..2000 {
82 number = hash(number);
83 let price = number % 10;
84
85 (a, b, c, d) = (b, c, d, to_index(previous, price));
87 let index = (6859 * a + 361 * b + 19 * c + d) as usize;
88 previous = price;
89
90 if seen[index] != id {
94 part_two[index] += price as u16;
95 seen[index] = id;
96 }
97 }
98
99 part_one += number as u64;
100 }
101
102 (part_one, part_two)
103 }
104
105 fn hash(mut n: u32) -> u32 {
108 n = (n ^ (n << 6)) & 0xffffff;
109 n = (n ^ (n >> 5)) & 0xffffff;
110 (n ^ (n << 11)) & 0xffffff
111 }
112
113 fn to_index(previous: u32, current: u32) -> u32 {
115 9 + current % 10 - previous % 10
116 }
117}
118
119#[cfg(feature = "simd")]
120mod implementation {
121 use std::simd::prelude::*;
122
123 use super::*;
124
125 type Vector = Simd<u32, 8>;
126
127 pub(super) fn parallel(input: &str) -> Vec<Result> {
128 let mut numbers: Vec<_> = input.iter_unsigned().collect();
129
130 numbers.resize(numbers.len().next_multiple_of(8), 0);
133 let chunks: Vec<_> = numbers.chunks_exact(8).collect();
134
135 spawn_parallel_iterator(&chunks, worker)
136 }
137
138 fn worker(iter: ParIter<'_, &[u32]>) -> Result {
142 let ten = Simd::splat(10);
143 let x = Simd::splat(6859);
144 let y = Simd::splat(361);
145 let z = Simd::splat(19);
146
147 let mut part_one = 0;
148 let mut part_two = vec![0; 130_321];
149
150 for slice in iter {
151 let mut seen = vec![u8::MAX; 130_321];
153
154 let zeroth = Simd::from_slice(slice);
155 let first = hash(zeroth);
156 let second = hash(first);
157 let third = hash(second);
158
159 let mut a;
160 let mut b = to_index(zeroth, first);
161 let mut c = to_index(first, second);
162 let mut d = to_index(second, third);
163
164 let mut number = third;
165 let mut previous = third % ten;
166
167 for _ in 3..2000 {
168 number = hash(number);
169 let prices = number % ten;
170
171 (a, b, c, d) = (b, c, d, to_index(previous, prices));
173 let indices = x * a + y * b + z * c + d;
174 previous = prices;
175
176 let indices = indices.to_array();
178 let prices = prices.to_array();
179
180 for i in 0..8 {
181 let index = indices[i] as usize;
182
183 let bit = (seen[index] >> i) & 1;
186 seen[index] &= !(1 << i);
187
188 part_two[index] += prices[i] as u16 * bit as u16;
189 }
190 }
191
192 part_one += number.reduce_sum() as u64;
193 }
194
195 (part_one, part_two)
196 }
197
198 #[inline]
201 fn hash(mut n: Vector) -> Vector {
202 let mask = Simd::splat(0xffffff);
203 n = (n ^ (n << 6)) & mask;
204 n = (n ^ (n >> 5)) & mask;
205 (n ^ (n << 11)) & mask
206 }
207
208 #[inline]
209 fn to_index(previous: Vector, current: Vector) -> Vector {
210 let nine = Simd::splat(9);
211 let ten = Simd::splat(10);
212 nine + (current % ten) - (previous % ten)
213 }
214}