-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
229 lines (216 loc) · 7.33 KB
/
lib.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// #![feature(stdsimd)]
use aoc::U8PtrExt;
// use std::arch::x86_64::{_mm256_loadu_epi32, _mm256_storeu_epi32, _mm256_sub_epi32};
const NUM_HISTORIES: usize = 200;
const VALUES_PER_HISTORY: usize = 21;
const NUM_VALUES: usize = NUM_HISTORIES * VALUES_PER_HISTORY;
type Data = [i32; NUM_VALUES + 201];
static mut DATA: Data = unsafe { std::mem::zeroed() };
pub fn p1(data: &Data) -> i32 {
let mut buffer: Data = unsafe { std::mem::zeroed() };
buffer.copy_from_slice(data);
predict_and_sum(&mut buffer)
}
pub fn p2(data: &Data) -> i32 {
let mut buffer: Data = unsafe { std::mem::zeroed() };
for (i, val) in data.iter().rev().skip(2).enumerate() {
buffer[i] = *val;
}
predict_and_sum(&mut buffer)
}
fn predict_and_sum(series: &mut Data) -> i32 {
for _ in 0..19 {
for i in 1..series.len() {
series[i - 1] = series[i].wrapping_sub(series[i - 1]);
}
}
let mut sum = 0;
let mut i = 0;
for _ in 0..NUM_HISTORIES {
sum += -series[i + 2];
i += VALUES_PER_HISTORY + 1;
}
sum
}
// fn predict_next_value_simd(series: &mut Data) -> i32 {
// unsafe {
// for pass in 0..19 {
// let mut i = 0;
// for _ in 0..550 {
// _mm256_storeu_epi32(
// series.as_mut_ptr().add(i),
// _mm256_sub_epi32(
// _mm256_loadu_epi32(series.as_ptr().add(i + 1)),
// _mm256_loadu_epi32(series.as_ptr().add(i)),
// ),
// );
// i += 8;
// }
// }
// }
// let mut sum = 0;
// let mut i = 0;
// for _ in 0..NUM_HISTORIES {
// sum += -series[i + 2];
// i += VALUES_PER_HISTORY + 1;
// }
// sum
// }
pub fn parse(input: &str) -> &'static Data {
let mut data = input.as_ptr();
unsafe {
let mut i = 0;
for _ in 0..NUM_HISTORIES {
// Unrolled loop 21 times. This seems to be the fastest way to parse the input.
DATA[i] = if data.read() == b'-' {
data = data.add(1);
-data.parse_uint::<i32, 1>()
} else {
data.parse_uint::<i32, 1>()
};
data = data.add(1);
DATA[i + 1] = if data.read() == b'-' {
data = data.add(1);
-data.parse_uint::<i32, 1>()
} else {
data.parse_uint::<i32, 1>()
};
data = data.add(1);
DATA[i + 2] = if data.read() == b'-' {
data = data.add(1);
-data.parse_uint::<i32, 1>()
} else {
data.parse_uint::<i32, 1>()
};
data = data.add(1);
DATA[i + 3] = if data.read() == b'-' {
data = data.add(1);
-data.parse_uint::<i32, 1>()
} else {
data.parse_uint::<i32, 1>()
};
data = data.add(1);
DATA[i + 4] = if data.read() == b'-' {
data = data.add(1);
-data.parse_uint::<i32, 1>()
} else {
data.parse_uint::<i32, 1>()
};
data = data.add(1);
DATA[i + 5] = if data.read() == b'-' {
data = data.add(1);
-data.parse_uint::<i32, 1>()
} else {
data.parse_uint::<i32, 1>()
};
data = data.add(1);
DATA[i + 6] = if data.read() == b'-' {
data = data.add(1);
-data.parse_uint::<i32, 1>()
} else {
data.parse_uint::<i32, 1>()
};
data = data.add(1);
DATA[i + 7] = if data.read() == b'-' {
data = data.add(1);
-data.parse_uint::<i32, 1>()
} else {
data.parse_uint::<i32, 1>()
};
data = data.add(1);
DATA[i + 8] = if data.read() == b'-' {
data = data.add(1);
-data.parse_uint::<i32, 1>()
} else {
data.parse_uint::<i32, 1>()
};
data = data.add(1);
DATA[i + 9] = if data.read() == b'-' {
data = data.add(1);
-data.parse_uint::<i32, 1>()
} else {
data.parse_uint::<i32, 1>()
};
data = data.add(1);
DATA[i + 10] = if data.read() == b'-' {
data = data.add(1);
-data.parse_uint::<i32, 1>()
} else {
data.parse_uint::<i32, 1>()
};
data = data.add(1);
DATA[i + 11] = if data.read() == b'-' {
data = data.add(1);
-data.parse_uint::<i32, 1>()
} else {
data.parse_uint::<i32, 1>()
};
data = data.add(1);
DATA[i + 12] = if data.read() == b'-' {
data = data.add(1);
-data.parse_uint::<i32, 1>()
} else {
data.parse_uint::<i32, 1>()
};
data = data.add(1);
DATA[i + 13] = if data.read() == b'-' {
data = data.add(1);
-data.parse_uint::<i32, 1>()
} else {
data.parse_uint::<i32, 1>()
};
data = data.add(1);
DATA[i + 14] = if data.read() == b'-' {
data = data.add(1);
-data.parse_uint::<i32, 1>()
} else {
data.parse_uint::<i32, 1>()
};
data = data.add(1);
DATA[i + 15] = if data.read() == b'-' {
data = data.add(1);
-data.parse_uint::<i32, 1>()
} else {
data.parse_uint::<i32, 1>()
};
data = data.add(1);
DATA[i + 16] = if data.read() == b'-' {
data = data.add(1);
-data.parse_uint::<i32, 1>()
} else {
data.parse_uint::<i32, 1>()
};
data = data.add(1);
DATA[i + 17] = if data.read() == b'-' {
data = data.add(1);
-data.parse_uint::<i32, 1>()
} else {
data.parse_uint::<i32, 1>()
};
data = data.add(1);
DATA[i + 18] = if data.read() == b'-' {
data = data.add(1);
-data.parse_uint::<i32, 1>()
} else {
data.parse_uint::<i32, 1>()
};
data = data.add(1);
DATA[i + 19] = if data.read() == b'-' {
data = data.add(1);
-data.parse_uint::<i32, 1>()
} else {
data.parse_uint::<i32, 1>()
};
data = data.add(1);
DATA[i + 20] = if data.read() == b'-' {
data = data.add(1);
-data.parse_uint::<i32, 1>()
} else {
data.parse_uint::<i32, 1>()
};
data = data.add(1);
i += VALUES_PER_HISTORY + 1;
}
&mut DATA
}
}