49 lines
950 B
Rust
49 lines
950 B
Rust
//! Day 1: Calorie counting
|
|
|
|
fn get_elves(input: Vec<String>) -> Vec<u32> {
|
|
let mut elves = Vec::new();
|
|
let mut buf = 0;
|
|
|
|
for line in input {
|
|
if line.is_empty() {
|
|
if buf > 0 {
|
|
elves.push(buf);
|
|
buf = 0;
|
|
}
|
|
} else {
|
|
buf += line.parse::<u32>().unwrap()
|
|
}
|
|
}
|
|
elves
|
|
}
|
|
|
|
pub fn part1(input: Vec<String>) -> String {
|
|
let elves = get_elves(input);
|
|
elves.iter().max().unwrap().to_string()
|
|
}
|
|
|
|
pub fn part2(input: Vec<String>) -> String {
|
|
let mut elves = get_elves(input);
|
|
elves.sort();
|
|
|
|
let mut acc = 0;
|
|
for i in 1..=3 {
|
|
acc += elves[elves.len() - i];
|
|
}
|
|
acc.to_string()
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn t_part1() {
|
|
assert_eq!(part1(crate::read_input(1)), "69177");
|
|
}
|
|
|
|
#[test]
|
|
fn t_part2() {
|
|
assert_eq!(part2(crate::read_input(1)), "207456");
|
|
}
|
|
}
|