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.
aoc2022/day1/src/main.rs

51 lines
1.1 KiB

use linebuffer::{
buf::forward,
ParsedLines,
};
use std::{
io,
str,
fs,
};
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Default, Copy)]
struct MaybeU64(Option<u64>);
impl str::FromStr for MaybeU64
{
type Err = <u64 as str::FromStr>::Err;
#[inline]
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.is_empty() {
Ok(Self(None))
} else {
Ok(Self(Some(s.parse()?)))
}
}
}
fn input_reader<R: io::Read>(input: R) -> ParsedLines<R, forward::FromStr<MaybeU64>>
{
ParsedLines::new(input)
}
fn main() -> Result<(), Box<dyn std::error::Error+ 'static>> {
let mut reader = input_reader(fs::OpenOptions::new().read(true).open("input")?);
let mut last_elf = 0;
let mut max_elf = 0;
while let Some(res) = reader.try_next()
{
match res.expect("Failed to parse line").0 {
Some(value) => last_elf += value,
None if last_elf > max_elf => {
max_elf = std::mem::replace(&mut last_elf, 0);
},
_ => last_elf = 0,
}
}
println!("{}", std::cmp::max(max_elf, last_elf));
Ok(())
}