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

35 lines
818 B
Rust
Raw Normal View History

2022-12-01 15:05:49 -05:00
use std::error::Error;
use std::fs;
fn main() -> Result<(), Box<dyn Error>> {
let file_str = fs::read_to_string("input.txt")?;
let mut elves: Vec<Vec<u32>> = Vec::new();
let mut totals: Vec<u32> = Vec::new();
for raw_elf in file_str.split("\n\n") {
let elf: Vec<u32> = raw_elf.split('\n')
.filter(|value| value.len() > 0)
.map(move |value| {
value.parse::<u32>().unwrap()
})
.collect();
let sum = elf
.clone()
.into_iter()
.reduce(|accum, item| accum + item)
.unwrap();
elves.push(elf);
totals.push(sum);
}
let most = totals.iter().max().unwrap();
println!("{:?}{:?}", elves, totals);
println!("Max calories: {}", most);
Ok(())
}