-
Notifications
You must be signed in to change notification settings - Fork 0
/
01.rs
53 lines (47 loc) · 988 Bytes
/
01.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
fn main() {
let input = include_str!("../../input/01.txt");
println!("Part 1: {}", part_1(input));
println!("Part 2: {}", part_2(input));
}
fn parse_input(input: &str) -> Vec<i64> {
input
.lines()
.map(|num| num.trim().parse::<i64>().unwrap())
.collect()
}
fn part_1(input: &str) -> i64 {
parse_input(input)
.windows(2)
.filter(|w| w[0] < w[1])
.count() as i64
}
fn part_2(input: &str) -> i64 {
parse_input(input)
.windows(4)
.filter(|w| w[0] < w[3])
.count() as i64
}
#[cfg(test)]
mod day_1_tests {
use super::*;
static INPUT: &str = "199
200
208
210
200
207
240
269
260
263";
#[test]
fn test_part_1() {
assert_eq!(part_1(INPUT), 7);
assert_eq!(1715, part_1(include_str!("../../input/01.txt")));
}
#[test]
fn test_part_2() {
assert_eq!(part_2(INPUT), 5);
assert_eq!(part_2(include_str!("../../input/01.txt")), 1739);
}
}