-
Notifications
You must be signed in to change notification settings - Fork 0
/
day09.rs
134 lines (103 loc) · 3.18 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
use crate::solutions::Solution;
pub struct Day09;
impl Solution for Day09 {
fn part_one(&self, input: &str) -> String {
let history = self.parse_input(input);
history
.iter()
.map(|h| self.calculate_at_the_end(h))
.sum::<i32>()
.to_string()
}
fn part_two(&self, input: &str) -> String {
let history = self.parse_input(input);
history
.iter()
.map(|h| self.calculate_at_the_beginning(h))
.sum::<i32>()
.to_string()
}
}
impl Day09 {
fn parse_input(&self, input: &str) -> Vec<Vec<i32>> {
input
.lines()
.map(|line| -> Vec<i32> {
line.split_whitespace()
.filter_map(|part| part.parse::<i32>().ok())
.collect()
})
.collect()
}
fn calculate_at_the_end(&self, history: &[i32]) -> i32 {
let differences = self.solve(history);
let mut result: i32 = 0;
for i in (0..differences.len() - 1).rev() {
let last = differences[i].last().unwrap();
result += last;
}
result
}
fn calculate_at_the_beginning(&self, history: &[i32]) -> i32 {
let differences = self.solve(history);
let mut result: i32 = 0;
for i in (0..differences.len() - 1).rev() {
let first = differences[i].first().unwrap();
result = first - result;
}
result
}
fn solve(&self, history: &[i32]) -> Vec<Vec<i32>> {
let mut differences: Vec<Vec<i32>> = vec![history.to_owned()];
loop {
let mut t: Vec<i32> = vec![];
let last = differences.last().unwrap();
for i in 1..last.len() {
t.push(last[i] - last[i - 1]);
}
differences.push(t.clone());
if t.iter().all(|n| n.eq(&0i32)) {
return differences;
}
}
}
}
#[cfg(test)]
mod tests {
use crate::file_system::read_example;
use crate::solutions::day09::Day09;
use crate::solutions::Solution;
#[test]
fn part_one_example_test() {
let input = read_example("09");
assert_eq!("114", Day09.part_one(input.as_str()));
}
#[test]
fn part_two_example_test() {
let input = read_example("09");
assert_eq!("2", Day09.part_two(input.as_str()));
}
#[test]
fn calculate_at_the_end() {
assert_eq!(18, Day09.calculate_at_the_end(&[0, 3, 6, 9, 12, 15]));
assert_eq!(28, Day09.calculate_at_the_end(&[1, 3, 6, 10, 15, 21]));
assert_eq!(68, Day09.calculate_at_the_end(&[10, 13, 16, 21, 30, 45]));
}
#[test]
fn calculate_at_the_beginning() {
assert_eq!(
5,
Day09.calculate_at_the_beginning(&[10, 13, 16, 21, 30, 45])
)
}
#[test]
fn parse_input() {
let input = read_example("09");
let expected: Vec<Vec<i32>> = vec![
vec![0, 3, 6, 9, 12, 15],
vec![1, 3, 6, 10, 15, 21],
vec![10, 13, 16, 21, 30, 45],
];
assert_eq!(expected, Day09.parse_input(input.as_str()));
}
}