-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday09.rs
77 lines (65 loc) · 1.77 KB
/
day09.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
fn extrapolate(history: Vec<i32>) -> i32 {
let mut components = vec![history];
while let Some(last) = components.last() {
if last.iter().all_equal() {
break;
}
components.push(last.windows(2).map(|p| p[1] - p[0]).collect());
}
components
.iter()
.map(|c| c.last().expect("failed to extrapolate line"))
.sum()
}
#[derive(Clone)]
struct Solution {
history: Vec<Vec<i32>>,
}
impl Solver for Solution {
fn new(input: &str) -> Anyhow<Self> {
Ok(Self {
history: input
.lines()
.map(|line| {
line.split(' ')
.map(|s| s.parse())
.collect::<ParseIntResult<Vec<_>>>()
})
.collect::<ParseIntResult<Vec<_>>>()?,
})
}
fn part1(&mut self) -> Anyhow<impl fmt::Display> {
Ok(self
.history
.par_iter()
.map(|h| extrapolate(h.to_vec()))
.sum::<i32>())
}
fn part2(&mut self) -> Anyhow<impl fmt::Display> {
Ok(self
.history
.par_iter()
.map(|h| extrapolate(h.iter().rev().cloned().collect()))
.sum::<i32>())
}
}
aoc::solution!();
#[cfg(test)]
mod test {
use super::{Solution, Solver};
const INPUT: &str = "0 3 6 9 12 15
1 3 6 10 15 21
10 13 16 21 30 45";
#[test]
fn test_part1() {
let mut solution = Solution::new(INPUT).unwrap();
let answer = solution.part1().unwrap().to_string();
assert_eq!(answer, "114");
}
#[test]
fn test_part2() {
let mut solution = Solution::new(INPUT).unwrap();
let answer = solution.part2().unwrap().to_string();
assert_eq!(answer, "2");
}
}