-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01.rs
64 lines (55 loc) · 1.18 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
63
64
/*
* Day 1: Calorie Counting
* See [https://adventofcode.com/2022/day/1]
*/
pub fn part_1(input: &str) -> Option<u32> {
let mut maxcal: u32 = 0;
let mut cal: u32 = 0;
for l in input.lines() {
if l.is_empty() {
if maxcal < cal {
maxcal = cal;
}
cal = 0;
} else {
let cc: u32 = l.parse().unwrap();
cal += cc;
}
}
if maxcal < cal {
maxcal = cal;
}
Some(maxcal)
}
pub fn part_2(input: &str) -> Option<u32> {
let mut elfc: Vec<u32> = Vec::new();
let mut cal: u32 = 0;
for l in input.lines() {
if l.is_empty() {
elfc.push(cal);
cal = 0;
} else {
let cc: u32 = l.parse().unwrap();
cal += cc;
}
}
if cal != 0 {
elfc.push(cal);
}
elfc.sort_by(|a, b| b.cmp(a));
Some(elfc.iter().take(3).sum())
}
aoc2022::solve!(part_1, part_2);
#[cfg(test)]
mod tests {
use aoc2022::assert_ex;
use super::*;
#[test]
fn test_part_1() {
assert_ex!(part_1, 24000);
}
#[test]
fn test_part_2() {
assert_ex!(part_2, 45000);
}
}