Very quick implementation for day 1

This commit is contained in:
2021-12-01 09:30:03 +01:00
parent 10531e3422
commit f413b08da6
4 changed files with 2075 additions and 4 deletions

2000
2021/inputs/01.txt Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -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);
}
}

View File

@@ -88,3 +88,10 @@ pub fn get_implementation(day: usize, part2: bool) -> Solution {
}
}
}
#[cfg(test)]
fn test_implementation(solution: Solution, data: impl AsRef<[u8]>, answer: impl ToString) {
let result = solution(&mut data.as_ref());
assert_eq!(answer.to_string(), result);
}

10
2021/src/samples/01.txt Normal file
View File

@@ -0,0 +1,10 @@
199
200
208
210
200
207
240
269
260
263