diff --git a/src/day1.rs b/src/day1.rs index d6f7190..7d3ef6c 100644 --- a/src/day1.rs +++ b/src/day1.rs @@ -8,44 +8,79 @@ pub fn run() { println!("\tPart 2: {}", part2(&input)); } -fn part1(food_list: &str) -> isize { - todo!() +fn part1(food_input: &str) -> usize { + food_input + .split("\n\n") + .collect::>() + .into_iter() + .map(|elf| { + elf.split_whitespace() + .map(|food| food.parse::().unwrap()) + .sum() + }) + .collect::>() + .into_iter() + .max() + .unwrap() } -fn part2(food_list: &str) -> usize { - todo!() +fn part2(food_input: &str) -> usize { + let mut elves_calories = food_input + .split("\n\n") + .collect::>() + .into_iter() + .map(|elf| { + elf.split_whitespace() + .map(|food| food.parse::().unwrap()) + .sum() + }) + .collect::>(); + + elves_calories.sort_by(|l, r| r.cmp(l)); + + elves_calories[..3].iter().sum::() } -// #[cfg(test)] -// mod tests { -// use super::*; +#[cfg(test)] +mod tests { + use super::*; -// #[test] -// fn test_1() { -// let mut input = "(())".to_string(); -// assert_eq!(part1(&input), 0); + #[test] + fn test_1() { + let input = "1000 +2000 +3000 -// input = "()()".to_string(); -// assert_eq!(part1(&input), 0); +4000 -// input = "(((".to_string(); -// assert_eq!(part1(&input), 3); +5000 +6000 -// input = "(()(()(".to_string(); -// assert_eq!(part1(&input), 3); +7000 +8000 +9000 -// input = "())".to_string(); -// assert_eq!(part1(&input), -1); +10000"; -// input = ")())())".to_string(); -// assert_eq!(part1(&input), -3); -// } + assert_eq!(part1(input), 24000); + } -// #[test] -// fn test_2() { -// let mut input = ")".to_string(); -// assert_eq!(part2(&input), 1); + #[test] + fn test_2() { + let input = "1000 +2000 +3000 -// input = "()())".to_string(); -// assert_eq!(part2(&input), 5); -// } -// } +4000 + +5000 +6000 + +7000 +8000 +9000 + +10000"; + + assert_eq!(part2(input), 45000); + } +}