-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday9.rs
58 lines (52 loc) · 1.2 KB
/
day9.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
fn predict(nums: &[i32]) -> i64 {
nums.iter()
.enumerate()
.fold((1, 0), |(c, s), (i, x)| {
(c * (nums.len() - i) / (i + 1), c as i64 * *x as i64 - s)
})
.1
}
pub fn part1(data: &str) -> i64 {
data.lines()
.map(|line| {
predict(
&line
.split_whitespace()
.filter_map(|s| s.parse::<i32>().ok())
.collect::<Vec<_>>(),
)
})
.sum()
}
pub fn part2(data: &str) -> i64 {
data.lines()
.map(|line| {
predict(
&line
.split_whitespace()
.rev()
.filter_map(|s| s.parse::<i32>().ok())
.collect::<Vec<_>>(),
)
})
.sum()
}
#[cfg(test)]
mod tests {
use super::*;
use indoc::indoc;
use pretty_assertions::assert_eq;
static EXAMPLE: &str = indoc! {"
0 3 6 9 12 15
1 3 6 10 15 21
10 13 16 21 30 45
"};
#[test]
fn part1_examples() {
assert_eq!(114, part1(EXAMPLE));
}
#[test]
fn part2_examples() {
assert_eq!(2, part2(EXAMPLE));
}
}