-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday_09.rs
86 lines (68 loc) · 1.61 KB
/
day_09.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
use common::{solution, Answer};
solution!("Mirage Maintenance", 9);
fn part_a(input: &str) -> Answer {
parse(input)
.iter()
.map(Sequence::predict)
.sum::<i64>()
.into()
}
fn part_b(input: &str) -> Answer {
parse(input)
.into_iter()
.map(|x| x.reverse().predict())
.sum::<i64>()
.into()
}
struct Sequence {
values: Vec<i64>,
}
fn parse(input: &str) -> Vec<Sequence> {
let mut out = Vec::new();
for line in input.lines() {
let values = line
.split_whitespace()
.map(|v| v.parse().unwrap())
.collect();
out.push(Sequence { values });
}
out
}
impl Sequence {
fn derive(&self) -> Vec<Vec<i64>> {
let mut derived = vec![self.values.clone()];
while !derived.last().unwrap().iter().all(|&x| x == 0) {
let last = derived.last().unwrap();
let mut next = Vec::new();
for i in 1..last.len() {
next.push(last[i] - last[i - 1]);
}
derived.push(next);
}
derived
}
fn reverse(mut self) -> Self {
self.values.reverse();
self
}
fn predict(&self) -> i64 {
self.derive().iter().filter_map(|v| v.last()).sum()
}
}
#[cfg(test)]
mod test {
use indoc::indoc;
const CASE: &str = indoc! {"
0 3 6 9 12 15
1 3 6 10 15 21
10 13 16 21 30 45
"};
#[test]
fn part_a() {
assert_eq!(super::part_a(CASE), 114.into());
}
#[test]
fn part_b() {
assert_eq!(super::part_b(CASE), 2.into());
}
}