This repository has been archived on 2023-12-13. You can view files and clone it, but cannot push or open issues or pull requests.
advent-of-code-2022/day1/src/main.rs

39 lines
1022 B
Rust
Raw Permalink Normal View History

2022-12-01 15:29:22 -05:00
fn get_elves(raw: &str) -> Vec<Vec<u32>> {
raw.split("\n\n")
.map(|raw_elf| {
raw_elf
.split('\n')
.filter(|value| value.len() > 0)
.map(move |value| value.parse::<u32>().unwrap())
.collect::<Vec<u32>>()
})
.collect()
}
2022-12-01 15:05:49 -05:00
2022-12-01 15:29:22 -05:00
fn get_elf_totals(elves: &Vec<Vec<u32>>) -> Vec<u32> {
2022-12-01 15:35:51 -05:00
elves
.clone()
.into_iter()
.map(|elf| {
elf.into_iter()
.reduce(|accum, item| accum + item)
.unwrap()
})
.collect()
2022-12-01 15:29:22 -05:00
}
2022-12-03 16:07:06 -05:00
fn main() {
let file_str = include_str!("input.txt"); //fs::read_to_string("input.txt")?;
2022-12-01 15:29:22 -05:00
let elves = get_elves(&file_str);
2022-12-01 15:35:51 -05:00
2022-12-01 15:29:22 -05:00
let mut totals: Vec<u32> = get_elf_totals(&elves);
totals.sort();
totals.reverse();
let most = totals[0];
let top3 = totals[0] + totals[1] + totals[2];
2022-12-01 15:05:49 -05:00
2022-12-01 15:29:22 -05:00
println!("Part 1: Most calories for one elf: {}", most);
println!("Part 2: Calories for top three elves: {}", top3);
2022-12-01 15:05:49 -05:00
}