From 4f67ce489105a7e7ef285c3b28a1a1316351dae8 Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Thu, 14 Dec 2023 08:36:42 +0100 Subject: [PATCH] mplement 2023 day 14 part 1 --- 2023/src/day14.rs | 36 ++++++++++++++++++++++++++++++++++-- 2023/src/samples/14.txt | 10 ++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 2023/src/samples/14.txt diff --git a/2023/src/day14.rs b/2023/src/day14.rs index 7c1760f..0211712 100644 --- a/2023/src/day14.rs +++ b/2023/src/day14.rs @@ -1,7 +1,39 @@ -pub fn part1(_input: &[u8]) -> anyhow::Result { - anyhow::bail!("Not implemented") +use crate::common::Grid; + +pub fn part1(input: &[u8]) -> anyhow::Result { + let grid = Grid::new(input)?; + let mut stack_heights = vec![0; grid.width()]; + let mut load = 0; + let height = grid.height(); + + for (y, row) in grid.rows().enumerate() { + for (&c, stack_height) in row.iter().zip(&mut stack_heights) { + match c { + b'#' => *stack_height = y + 1, + b'O' => { + load += height - *stack_height; + *stack_height += 1; + } + _ => continue, + } + } + } + + Ok(load.to_string()) } pub fn part2(_input: &[u8]) -> anyhow::Result { anyhow::bail!("Not implemented") } + +#[cfg(test)] +mod tests { + use super::*; + + const SAMPLE: &[u8] = include_bytes!("samples/14.txt"); + + #[test] + fn sample_part1() { + assert_eq!("136", part1(SAMPLE).unwrap()); + } +} diff --git a/2023/src/samples/14.txt b/2023/src/samples/14.txt new file mode 100644 index 0000000..462ca7c --- /dev/null +++ b/2023/src/samples/14.txt @@ -0,0 +1,10 @@ +OOOO.#.O.. +OO..#....# +OO..O##..O +O..#.OO... +........#. +..#....#.# +..O..#.O.O +..O....... +#....###.. +#....#....