[rs] Solve 2022_01
This commit is contained in:
parent
583cf9322a
commit
569e659342
2 changed files with 2303 additions and 1 deletions
2275
rs/inputs/d01.txt
Normal file
2275
rs/inputs/d01.txt
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -1,4 +1,31 @@
|
|||
pub fn solve(input: String) -> anyhow::Result<()> {
|
||||
println!("TODO!");
|
||||
let mut elves = vec![];
|
||||
let mut elf = vec![];
|
||||
for line in input.lines() {
|
||||
if let Ok(number) = line.trim().parse::<u32>() {
|
||||
elf.push(number);
|
||||
} else {
|
||||
elves.push(elf);
|
||||
elf = vec![];
|
||||
}
|
||||
}
|
||||
if !elf.is_empty() {
|
||||
elves.push(elf);
|
||||
}
|
||||
|
||||
let mut elves = elves
|
||||
.into_iter()
|
||||
.map(|e| e.into_iter().sum())
|
||||
.collect::<Vec<u32>>();
|
||||
elves.sort_unstable();
|
||||
|
||||
// Part 1
|
||||
let top = elves.last().unwrap();
|
||||
println!("Part 1: {top}");
|
||||
|
||||
// Part 2
|
||||
let top_three = elves.iter().rev().take(3).sum::<u32>();
|
||||
println!("Part 2: {top_three}");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue