From 18bdad10c27d199a4085655ce93902d4bba0d534 Mon Sep 17 00:00:00 2001 From: Justin Wernick Date: Wed, 1 Dec 2021 08:44:23 +0200 Subject: Day 1 --- src/bin/day_1.rs | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 1 + src/parsers.rs | 6 +++++ 3 files changed, 85 insertions(+) create mode 100644 src/bin/day_1.rs create mode 100644 src/lib.rs create mode 100644 src/parsers.rs (limited to 'src') diff --git a/src/bin/day_1.rs b/src/bin/day_1.rs new file mode 100644 index 0000000..7b89d69 --- /dev/null +++ b/src/bin/day_1.rs @@ -0,0 +1,78 @@ +use aoc2021::parsers; +use nom::{character::complete::line_ending, combinator::map, multi::separated_list1, IResult}; +use std::fs; + +fn main() -> Result<(), Box> { + let input = fs::read_to_string("inputs/day_1.txt")?; + let sonar_scan = parse_sonar_scan(&input).unwrap().1; + + { + let mut simple_increase_counter = DepthIncreaseCounter::default(); + for (depth, next_depth) in sonar_scan.iter().zip(sonar_scan.iter().skip(1)) { + if next_depth > depth { + simple_increase_counter.increment(); + } + } + dbg!(simple_increase_counter); + } + + { + let windowed_sonar_scan = sonar_scan + .iter() + .zip(sonar_scan.iter().skip(1)) + .zip(sonar_scan.iter().skip(2)) + .map(|((depth1, depth2), depth3)| ThreeDepthWindowSum::new([*depth1, *depth2, *depth3])) + .collect::>(); + + let mut windowed_increase_counter = DepthIncreaseCounter::default(); + for (depth, next_depth) in windowed_sonar_scan + .iter() + .zip(windowed_sonar_scan.iter().skip(1)) + { + if next_depth > depth { + windowed_increase_counter.increment(); + } + } + dbg!(windowed_increase_counter); + } + + Ok(()) +} + +#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)] +struct Depth(u64); + +#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)] +struct ThreeDepthWindowSum(u64); + +impl ThreeDepthWindowSum { + fn new(depths: [Depth; 3]) -> ThreeDepthWindowSum { + ThreeDepthWindowSum(depths.into_iter().map(|d| d.0).sum()) + } +} + +fn parse_sonar_scan(input: &str) -> IResult<&str, Vec> { + separated_list1(line_ending, parse_depth)(input) +} + +fn parse_depth(input: &str) -> IResult<&str, Depth> { + map(parsers::u64, |number| Depth(number))(input) +} + +#[derive(Default, Debug)] +struct DepthIncreaseCounter(u64); + +impl DepthIncreaseCounter { + fn increment(&mut self) { + self.0 += 1; + } +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn parses_a_depth() { + assert_eq!(parse_depth("96\n"), Ok(("\n", Depth(96)))); + } +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..be756a0 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1 @@ +pub mod parsers; diff --git a/src/parsers.rs b/src/parsers.rs new file mode 100644 index 0000000..27b2aad --- /dev/null +++ b/src/parsers.rs @@ -0,0 +1,6 @@ +use nom::{character::complete::digit1, combinator::map_res, IResult}; +use std::str::FromStr; + +pub fn u64(input: &str) -> IResult<&str, u64> { + map_res(digit1, |number| u64::from_str(number))(input) +} -- cgit v1.2.3