Advent of Code 2022
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
990 B

use std::fs::File;
use std::io::BufRead;
use std::io::BufReader;
use clap::Arg;
use clap::Command;
fn main() {
let matches = Command::new("Day1").arg(Arg::new("filename")).get_matches();
let filename = matches
.get_one::<String>("filename")
.expect("Need a filename");
println!("Processing {}...", filename);
let file = File::open(filename).expect("Can't read file");
let reader = BufReader::new(file);
let mut max_calories = 0;
let mut elf_calories = 0;
for line in reader.lines() {
let line = line.unwrap();
let line = line.trim();
// XXX I want to do this with an iterator!
if line.is_empty() {
if elf_calories > max_calories {
max_calories = elf_calories;
}
elf_calories = 0;
} else {
println!("Line: {}", line);
elf_calories += line.parse::<u32>().unwrap();
}
}
println!("Max: {}", max_calories);
}