-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.rs
39 lines (35 loc) · 1.19 KB
/
main.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
use smallvec::{smallvec, SmallVec};
const MAX: usize = 21;
pub fn main() {
let nums = include_bytes!("../input.txt")
.split(|b| b == &b'\n')
.map(|line| {
line.split(|b| b == &b' ')
.map(|b| atoi::atoi(b).unwrap())
.collect::<SmallVec<[_; 21]>>()
})
.collect::<SmallVec<[_; 200]>>();
// <https://en.wikipedia.org/wiki/Binomial_coefficient#Pascal's_triangle>
let mut triang: SmallVec<[SmallVec<[i64; MAX + 1]>; MAX]> = smallvec![smallvec![1]];
for i in 0..MAX {
let mut next = smallvec![1];
next.extend(triang[i].windows(2).map(|w| w[0] + w[1]).chain([1]));
triang.push(next);
}
(0..=MAX)
.flat_map(|row| (0..=row).step_by(2).map(move |col| (row, col)))
.for_each(|(row, col)| triang[row][col] *= -1);
println!(
"{}",
nums.into_iter()
.map(|nums| {
let row = nums.len();
nums.iter()
.enumerate()
.map(|(col, n)| triang[row][col] * n)
.sum::<i64>()
* if row % 2 == 0 { 1 } else { -1 }
})
.sum::<i64>()
);
}