-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution_2021_01.rs
61 lines (50 loc) · 1.19 KB
/
solution_2021_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
54
55
56
57
58
59
60
61
use itertools::izip;
fn parse(input: &str) -> Vec<u32> {
input
.lines()
.filter(|x| !x.is_empty())
.map(|x| x.parse::<u32>().unwrap())
.collect()
}
fn count_larger(data: &[u32]) -> usize {
data[0 .. data.len() - 1]
.iter()
.zip(&data[1 ..])
.filter(|(a, b)| a < b)
.count()
}
fn part_1(input: &str) -> usize {
let data: Vec<_> = parse(input);
count_larger(&data)
}
fn part_2(input: &str) -> usize {
let data: Vec<_> = parse(input);
let a = &data[0 .. data.len() - 2];
let b = &data[1 .. data.len() - 1];
let c = &data[2 ..];
let sums: Vec<_> = izip!(a, b, c).map(|(x, y, z)| x + y + z).collect();
count_larger(&sums)
}
fn main() {
let test_data = "199
200
208
210
200
207
240
269
260
263";
let test_result_1 = part_1(test_data);
assert_eq!(test_result_1, 7);
let data = include_str!("../../resources/01.txt");
let result_1 = part_1(data);
assert_eq!(result_1, 1681);
println!("Part 1: {result_1}");
let test_result_2 = part_2(test_data);
assert_eq!(test_result_2, 5);
let result_2 = part_2(data);
assert_eq!(result_2, 1704);
println!("Part 2: {result_2}");
}