-
Notifications
You must be signed in to change notification settings - Fork 0
/
day09.rs
57 lines (48 loc) · 1.44 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
#[aoc_generator(day9)]
pub fn input_generator(input: &str) -> Vec<Vec<i32>> {
input
.lines()
.map(|line| {
line
.trim()
.split_whitespace()
.map(|num| num.parse::<i32>().unwrap())
.collect()
})
.collect()
}
// Hmm... I don't like this redundancy, but I also don't like recursive calls...
fn extrapolate(series: &Vec<i32>) -> i32 {
let mut all_zeroes = true;
let mut deltas: Vec<i32> = series.windows(2)
.map(|window| {
let delta = window[1] - window[0];
all_zeroes = all_zeroes && delta == 0;
delta
})
.collect();
let mut end_num = *series.last().unwrap();
while !all_zeroes {
let current_deltas = deltas.windows(2)
.map(|window| {
let delta = window[1] - window[0];
all_zeroes = all_zeroes && delta == 0;
delta
})
.collect();
if deltas.is_empty() {
break;
}
end_num += deltas.last().unwrap_or(&0);
deltas = current_deltas;
}
end_num
}
#[aoc(day9, part1)]
pub fn solve_part1(input: &Vec<Vec<i32>>) -> i32 {
input.iter().map(|series| extrapolate(&series)).sum()
}
#[aoc(day9, part2)]
pub fn solve_part2(input: &Vec<Vec<i32>>) -> i32 {
input.iter().map(|series| extrapolate(&series.iter().rev().cloned().collect())).sum()
}