-
Notifications
You must be signed in to change notification settings - Fork 0
/
01.rs
62 lines (50 loc) · 1.05 KB
/
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
62
use itertools::Itertools;
static INPUT_TXT: &str = include_str!("../../input/01.txt");
fn main() {
println!("Part 1: {}", part_1(INPUT_TXT));
println!("Part 2: {}", part_2(INPUT_TXT));
}
fn parse_input(input: &str) -> Vec<i64> {
input
.split("\n\n")
.map(|elf| {
elf.lines()
.map(|num| num.trim().parse::<i64>().unwrap())
.sum()
})
.collect()
}
fn part_1(input: &str) -> i64 {
parse_input(input).into_iter().max().unwrap()
}
fn part_2(input: &str) -> i64 {
parse_input(input)
.into_iter()
.sorted_by(|a, b| Ord::cmp(&b, &a))
.take(3)
.sum()
}
#[cfg(test)]
mod day_1_tests {
use super::*;
static INPUT: &str = "1000
2000
3000
4000
5000
6000
7000
8000
9000
10000";
#[test]
fn test_part_1() {
assert_eq!(part_1(INPUT), 24000);
assert_eq!(part_1(INPUT_TXT), 66186);
}
#[test]
fn test_part_2() {
assert_eq!(part_2(INPUT), 45000);
assert_eq!(part_2(INPUT_TXT), 196_804);
}
}