-
Notifications
You must be signed in to change notification settings - Fork 4
/
day1.rs
62 lines (55 loc) · 1.29 KB
/
day1.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 std::cmp::Reverse;
use std::vec::Vec;
fn parse<'a, I, S>(lines: I) -> Vec<u32>
where
I: IntoIterator<Item = &'a S>,
S: AsRef<str> + 'a,
{
let mut sums = Vec::new();
let mut sum = 0;
for line in lines {
match line.as_ref().parse::<u32>() {
Ok(num) => sum += num,
Err(_) => {
sums.push(sum);
sum = 0;
}
}
}
sums.push(sum);
sums
}
pub fn part1<'a, I, S>(lines: I) -> u32
where
I: IntoIterator<Item = &'a S>,
S: AsRef<str> + 'a,
{
let nums = parse(lines);
nums.into_iter().max().unwrap_or(0)
}
pub fn part2<'a, I, S>(lines: I) -> u32
where
I: IntoIterator<Item = &'a S>,
S: AsRef<str> + 'a,
{
let mut nums = parse(lines);
let (maxs, &mut max, _) = nums[..].select_nth_unstable_by_key(2, |&num| Reverse(num));
maxs.iter_mut().fold(max, |x, &mut y| x + y)
}
#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;
static EXAMPLE: &[&str] = &[
"1000", "2000", "3000", "", "4000", "", "5000", "6000", "", "7000", "8000", "9000", "",
"10000",
];
#[test]
fn part1_examples() {
assert_eq!(24000, part1(EXAMPLE));
}
#[test]
fn part2_examples() {
assert_eq!(45000, part2(EXAMPLE));
}
}