mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-26 21:30:31 +01:00
Rework day 1
Simplify part 2 a lot, by not actually computing the sums because they do not matter, only the changes do. Also eliminate the allocation overhead while parsing line-by-line input. Fixes the existing clippy error because the offending line no longer exists.
This commit is contained in:
45
2021/src/common.rs
Normal file
45
2021/src/common.rs
Normal file
@@ -0,0 +1,45 @@
|
||||
use std::io::BufRead;
|
||||
use std::io::BufReader;
|
||||
use std::io::Read;
|
||||
use std::marker::PhantomData;
|
||||
use std::str::FromStr;
|
||||
|
||||
/// Line-based iterator/parser
|
||||
///
|
||||
/// For each line of the input, attempt to parse it as the requested type. Iteration is stopped on
|
||||
/// the first IO error or parse error, silently. Leading and trailing whitespace is stripped before
|
||||
/// attempting to parse.
|
||||
pub struct LineParser<'a, I>
|
||||
where
|
||||
I: FromStr,
|
||||
{
|
||||
reader: BufReader<&'a mut dyn Read>,
|
||||
buffer: String,
|
||||
_data: PhantomData<I>,
|
||||
}
|
||||
|
||||
impl<'a, I: FromStr> LineParser<'a, I> {
|
||||
pub fn new(input: &'a mut dyn Read) -> Self {
|
||||
Self {
|
||||
reader: BufReader::new(input),
|
||||
buffer: String::new(),
|
||||
_data: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
fn next_line(&mut self) -> Option<&str> {
|
||||
self.buffer.clear();
|
||||
|
||||
self.reader.read_line(&mut self.buffer).ok()?;
|
||||
|
||||
Some(self.buffer.trim())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, I: FromStr> Iterator for LineParser<'a, I> {
|
||||
type Item = I;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
self.next_line()?.parse().ok()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user