mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Merge day 1 solutions
This commit is contained in:
@@ -2,28 +2,22 @@ use std::io::Read;
|
||||
|
||||
use crate::common::LineParser;
|
||||
|
||||
fn read_input(input: &mut dyn Read) -> Vec<u32> {
|
||||
LineParser::new(input).collect()
|
||||
fn part_generic(input: &mut dyn Read, window: usize) -> String {
|
||||
let numbers: Vec<u32> = LineParser::new(input).collect();
|
||||
|
||||
numbers
|
||||
.windows(window)
|
||||
.filter(|w| w.last() > w.first())
|
||||
.count()
|
||||
.to_string()
|
||||
}
|
||||
|
||||
pub fn part1(input: &mut dyn Read) -> String {
|
||||
let numbers = read_input(input);
|
||||
|
||||
numbers
|
||||
.windows(2)
|
||||
.filter(|w| w[1] > w[0])
|
||||
.count()
|
||||
.to_string()
|
||||
part_generic(input, 2)
|
||||
}
|
||||
|
||||
pub fn part2(input: &mut dyn Read) -> String {
|
||||
let numbers = read_input(input);
|
||||
|
||||
numbers
|
||||
.windows(4)
|
||||
.filter(|w| w[3] > w[0])
|
||||
.count()
|
||||
.to_string()
|
||||
part_generic(input, 4)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
Reference in New Issue
Block a user