-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.rs
37 lines (32 loc) · 1.09 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
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::<i64>(b).unwrap())
.collect::<SmallVec<[_; MAX]>>()
})
.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);
let mut answer = 0;
for nums in nums {
let row = nums.len();
answer += nums
.iter()
.enumerate()
.map(|(col, n)| triang[row][col + 1] * n)
.sum::<i64>();
}
println!("{answer}");
}