-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
54 lines (47 loc) · 1.18 KB
/
main.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
use itertools::Itertools;
pub fn main() {
let data = include_str!("input.txt");
println!("Part 1: Max calories is {}", part_one(data));
println!("Part 2: Top 3 calories is {}", part_two(data));
}
fn part_one(data: &str) -> u64 {
data.lines()
.map(|v| v.parse::<u64>().ok())
.batching(|it| {
let mut sum = None;
while let Some(Some(v)) = it.next() {
sum = Some(sum.unwrap_or(0) + v);
}
sum
})
.max()
.unwrap_or_default()
}
fn part_two(data: &str) -> u64 {
data.lines()
.map(|v| v.parse::<u64>().ok())
.batching(|it| {
let mut sum = None;
while let Some(Some(v)) = it.next() {
sum = Some(sum.unwrap_or(0) + v);
}
sum
})
.sorted_by_key(|&v| std::cmp::Reverse(v))
.take(3)
.sum()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn one() {
let data = include_str!("test.txt");
assert_eq!(24_000, part_one(data));
}
#[test]
fn two() {
let data = include_str!("test.txt");
assert_eq!(45_000, part_two(data));
}
}