mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-27 05:40:32 +01:00
Very quick implementation for day 1
This commit is contained in:
@@ -1,9 +1,63 @@
|
||||
use std::io::BufRead;
|
||||
use std::io::BufReader;
|
||||
use std::io::Read;
|
||||
|
||||
pub fn part1(_input: &mut dyn Read) -> String {
|
||||
todo!()
|
||||
fn read_input(input: &mut dyn Read) -> Vec<u32> {
|
||||
let reader = BufReader::new(input);
|
||||
|
||||
// TODO: optimize allocations out
|
||||
reader
|
||||
.lines()
|
||||
.map(|l| l.unwrap().parse().unwrap())
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn part2(_input: &mut dyn Read) -> String {
|
||||
todo!()
|
||||
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()
|
||||
}
|
||||
|
||||
pub fn part2(input: &mut dyn Read) -> String {
|
||||
let numbers = read_input(input);
|
||||
|
||||
let mut last = None;
|
||||
|
||||
numbers
|
||||
.windows(3)
|
||||
.filter(|w| {
|
||||
let sum: u32 = w.iter().sum();
|
||||
|
||||
let prev = last.replace(sum);
|
||||
|
||||
match prev {
|
||||
Some(n) if n < sum => true,
|
||||
_ => false,
|
||||
}
|
||||
})
|
||||
.count()
|
||||
.to_string()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::test_implementation;
|
||||
|
||||
use super::*;
|
||||
|
||||
const SAMPLE: &[u8] = include_bytes!("samples/01.txt");
|
||||
|
||||
#[test]
|
||||
fn sample_part1() {
|
||||
test_implementation(part1, SAMPLE, 7);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sample_part2() {
|
||||
test_implementation(part2, SAMPLE, 5);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user