heh
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs40
1 files changed, 15 insertions, 25 deletions
diff --git a/src/main.rs b/src/main.rs
index fea8e8f..d878a4e 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -9,39 +9,29 @@
)]
extern crate test;
mod util;
-use std::mem::ManuallyDrop;
pub use util::prelude::*;
-pub fn p1(a: &[i32]) -> i32 {
- if !a[1..].iter().any(|&x| x != a[0]) {
- a[0]
- } else {
- *a.last().unwrap()
- + p1(&*ManuallyDrop::new(
- a.array_windows::<2>()
- .map(|[b, a]| a - b)
- .collect::<Box<[_]>>(),
- ))
- }
-}
+/// generated by passing 1, 0{20} and 0, 1, 0{19}, etc to the original
+const INTERPOLATION_P1: [i32; 21] = [
+ 1, -21, 210, -1330, 5985, -20349, 54264, -116280, 203490, -293930, 352716, -352716, 293930,
+ -203490, 116280, -54264, 20349, -5985, 1330, -210, 21,
+];
+/// just the reversed version of [`INTERPOLATION_P1`]
+const INTERPOLATION_P2: [i32; 21] = [
+ 21, -210, 1330, -5985, 20349, -54264, 116280, -203490, 293930, -352716, 352716, -293930,
+ 203490, -116280, 54264, -20349, 5985, -1330, 210, -21, 1,
+];
-pub fn p2(a: &[i32]) -> i32 {
- if !a[1..].iter().any(|&x| x != a[0]) {
- a[0]
- } else {
- *a.first().unwrap()
- - p2(&*ManuallyDrop::new(
- a.array_windows::<2>()
- .map(|[b, a]| a - b)
- .collect::<Box<[_]>>(),
- ))
- }
+pub fn solve(a: impl Iterator<Item = i32>, interp: [i32; 21]) -> i32 {
+ a.zip(interp.into_iter())
+ .map(|(a, b)| a as i64 * b as i64)
+ .sum::<i64>() as i32
}
pub fn run(i: &str) -> impl Display {
i.行()
- .map(|x| p1(&*ManuallyDrop::new(x.κ().collect::<Box<[i32]>>())))
+ .map(|x| solve(&mut x.κ(), INTERPOLATION_P2))
.sum::<i32>()
}