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;
|
use crate::common::LineParser;
|
||||||
|
|
||||||
fn read_input(input: &mut dyn Read) -> Vec<u32> {
|
fn part_generic(input: &mut dyn Read, window: usize) -> String {
|
||||||
LineParser::new(input).collect()
|
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 {
|
pub fn part1(input: &mut dyn Read) -> String {
|
||||||
let numbers = read_input(input);
|
part_generic(input, 2)
|
||||||
|
|
||||||
numbers
|
|
||||||
.windows(2)
|
|
||||||
.filter(|w| w[1] > w[0])
|
|
||||||
.count()
|
|
||||||
.to_string()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn part2(input: &mut dyn Read) -> String {
|
pub fn part2(input: &mut dyn Read) -> String {
|
||||||
let numbers = read_input(input);
|
part_generic(input, 4)
|
||||||
|
|
||||||
numbers
|
|
||||||
.windows(4)
|
|
||||||
.filter(|w| w[3] > w[0])
|
|
||||||
.count()
|
|
||||||
.to_string()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|||||||
Reference in New Issue
Block a user