-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_1.rs
128 lines (109 loc) · 3.15 KB
/
day_1.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
use std::{error::Error, io::Read};
pub fn solve_part_1() {
match read_input(&mut std::io::stdin()) {
Ok(values) => println!("{:?}", count_increases(values)),
Err(err) => println!("Could not read input: {:?}", err),
}
}
pub fn solve_part_2() {
match read_input(&mut std::io::stdin()) {
Ok(values) => println!("{:?}", count_sliding_window_increases(values)),
Err(err) => println!("Could not read input: {:?}", err),
}
}
/// Reads the input from the given input stream. It expects a string of multiple
/// lines with integers.
fn read_input(reader: &mut impl Read) -> Result<Vec<u32>, Box<dyn Error>> {
let mut buffer = String::new();
reader.read_to_string(&mut buffer)?;
buffer.trim().lines().map(|l| Ok(l.parse()?)).collect()
}
/// Counts the number of increases compared to the previous value in the given
/// vector.
fn count_increases(values: Vec<u32>) -> u32 {
if values.is_empty() {
return 0;
}
let mut increases = 0;
let mut last_value = values.first().unwrap();
for value in values.iter().skip(1) {
if value > last_value {
increases += 1;
}
last_value = value;
}
increases
}
/// Counts the increases of the sum of values in a sliding window with three
/// measurements with the previous window.
fn count_sliding_window_increases(values: Vec<u32>) -> u32 {
if values.is_empty() {
return 0;
}
let mut increases = 0;
let mut last_value = *values.first().unwrap();
for (i, value) in values.iter().enumerate() {
if i >= values.len() - 3 {
break;
}
let window_value = *value + values[i + 1] + values[i + 2];
if window_value > last_value {
increases += 1;
}
last_value = window_value;
}
increases
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn reads_and_parses_example_input() {
let input = "199
200
208
210
200
207
240
269
260
263
";
let values = read_input(&mut input.as_bytes()).unwrap();
assert_eq!(
values,
vec![199, 200, 208, 210, 200, 207, 240, 269, 260, 263]
);
}
#[test]
fn reads_and_parses_empty_input() {
let input = "";
let result = read_input(&mut input.as_bytes()).unwrap();
assert_eq!(result, vec![]);
}
#[test]
fn count_increases_with_example_values() {
let values = vec![199, 200, 208, 210, 200, 207, 240, 269, 260, 263];
let result = count_increases(values);
assert_eq!(result, 7);
}
#[test]
fn count_increases_with_empty_values() {
let values = vec![];
let result = count_increases(values);
assert_eq!(result, 0);
}
#[test]
fn count_sliding_window_increases_with_example_values() {
let values = vec![199, 200, 208, 210, 200, 207, 240, 269, 260, 263];
let result = count_sliding_window_increases(values);
assert_eq!(result, 5);
}
#[test]
fn count_sliding_window_increases_with_empty_values() {
let values = vec![];
let result = count_sliding_window_increases(values);
assert_eq!(result, 0);
}
}