From d6ca0b2b0c17a9afd9d040c6753b451d90db5ea8 Mon Sep 17 00:00:00 2001 From: LeMoonStar Date: Wed, 4 Dec 2024 07:28:02 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20WIP=20on=20Day=204=20I=20don't?= =?UTF-8?q?=20have=20enough=20time=20in=20the=20morning...?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/days/d04.rs | 125 ++++++++++++++++++++++++++++++-- src/days/test_inputs/test04.txt | 10 +++ 2 files changed, 127 insertions(+), 8 deletions(-) diff --git a/src/days/d04.rs b/src/days/d04.rs index 0cda657..192589d 100644 --- a/src/days/d04.rs +++ b/src/days/d04.rs @@ -2,31 +2,140 @@ use super::{Answer, Day, DayImpl}; const CURRENT_DAY: u8 = 4; -type Data = Vec; +#[derive(Debug, Clone,Copy)] +enum Direction { + East, + West, + North, + South, + NorthEast, + NorthWest, + SouthEast, + SouthWest +} + +#[derive(Debug, Clone)] +pub struct LetterWall (Vec>); + +impl LetterWall { + fn at(&self, x: usize, y: usize) -> Option<&char> { + let row = self.0.get(y); + + if let Some(row) = row { + row.get(x) + } else { + None + } + } + + pub fn is_upper_left_of_xmas(&self, x: usize, y: usize) -> Option { + match( + self.at( x, y), self.at( x+1, y), self.at( x+2, y), self.at( x+3, y), + self.at( x, y+1), self.at( x+1, y+1), self.at( x+2, y+1), self.at( x+3, y+1), + self.at( x, y+2), self.at( x+1, y+2), self.at( x+2, y+2), self.at( x+3, y+2), + self.at( x, y+3), self.at( x+1, y+3), self.at( x+2, y+3), self.at( x+3, y+3) + ) { + ( + Some('X'), Some('M'), Some('A'), Some('S'), + _, _, _, _, + _, _, _, _, + _, _, _, _ + ) => Some(Direction::East), + ( + Some('S'), Some('A'), Some('M'), Some('X'), + _, _, _, _, + _, _, _, _, + _, _, _, _ + ) => Some(Direction::West), + ( + Some('X'), _, _, _, + Some('M'), _, _, _, + Some('A'), _, _, _, + Some('S'), _, _, _ + ) => Some(Direction::South), + ( + Some('S'), _, _, _, + Some('A'), _, _, _, + Some('M'), _, _, _, + Some('X'), _, _, _ + ) => Some(Direction::North), + ( + Some('X'), _, _, _, + _, Some('M'), _, _, + _, _, Some('A'), _, + _, _, _, Some('S') + ) => Some(Direction::SouthEast), + ( + Some('S'), _, _, _, + _, Some('A'), _, _, + _, _, Some('M'), _, + _, _, _, Some('X') + ) => Some(Direction::NorthWest), + ( + _, _, _, Some('X'), + _, _, Some('M'), _, + _, Some('A'), _, _, + Some('S'), _, _, _ + ) => Some(Direction::SouthWest), + ( + _, _, _, Some('S'), + _, _, Some('A'), _, + _, Some('M'), _, _, + Some('X'), _, _, _ + ) => Some(Direction::SouthWest), + _ => None + } + } + + pub fn count_xmas(&self) -> u64 { + let mut count = 0; + for y in 0..self.0.len(){ + for x in 0..self.0[0].len() { + if let Some(dir) = self.is_upper_left_of_xmas(x, y) { + println!("FOUND AT {} {} '{}' - DIR: {:?}", x, y, self.0[y][x], dir); + count += 1; + } + } + } + return count; + } +} + +impl From<&str> for LetterWall { + fn from(value: &str) -> Self { + Self ( + value + .lines() + .map(|v| v.chars().collect()) + .collect() + ) + } +} + +type Data = LetterWall; + impl DayImpl for Day { fn init_test() -> (Self, Data) { Self::init(include_str!("test_inputs/test04.txt")) } fn expected_results() -> (Answer, Answer) { - (Answer::Number(0), Answer::Number(0)) + (Answer::Number(18), Answer::Number(0)) } fn init(input: &str) -> (Self, Data) { ( Self {}, - input - .lines() - .map(|v| v.parse::().expect("error while parsing input.")) - .collect(), + input.into(), ) } fn one(&self, data: &mut Data) -> Answer { - Answer::Number(data.len() as u64) + // NOTE TO SELF: Actual result above 2334 + Answer::Number(data.count_xmas()) } fn two(&self, data: &mut Data) -> Answer { - Answer::Number(data.len() as u64) + Answer::Number(0) } } diff --git a/src/days/test_inputs/test04.txt b/src/days/test_inputs/test04.txt index e69de29..1f4eda2 100644 --- a/src/days/test_inputs/test04.txt +++ b/src/days/test_inputs/test04.txt @@ -0,0 +1,10 @@ +MMMSXXMASM +MSAMXMSMSA +AMXSXMAAMM +MSAMASMSMX +XMASAMXAMM +XXAMMXXAMA +SMSMSASXSS +SAXAMASAAA +MAMMMXMMMM +MXMXAXMASX