From 134512e4ba80e67e2ceb386cdf5a30468b028105 Mon Sep 17 00:00:00 2001 From: LeMoonStar Date: Wed, 4 Dec 2024 19:33:28 +0100 Subject: [PATCH 1/3] =?UTF-8?q?=E2=9C=A8=20Day=204:=20Solved=20part=201!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/days/d04.rs | 147 ++++++++++++++++++++++++++++-------------------- 1 file changed, 85 insertions(+), 62 deletions(-) diff --git a/src/days/d04.rs b/src/days/d04.rs index 5fd34eb..564ed4b 100644 --- a/src/days/d04.rs +++ b/src/days/d04.rs @@ -30,77 +30,100 @@ impl LetterWall { } } - fn is_upper_left_of_xmas(&self, x: usize, y: usize) -> Option { - #[rustfmt::skip] - 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::NorthEast), - _ => None + #[rustfmt::skip] + fn get_xmas_directions(&self, x: usize, y: usize) -> Vec { + let mut directions = vec!(); + + let map = ( + 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) + ); + + if let ( + Some(&'X'), Some(&'M'), Some(&'A'), Some(&'S'), + _, _, _, _, + _, _, _, _, + _, _, _, _ + ) = map { + directions.push(Direction::East); } + + if let ( + Some('S'), Some('A'), Some('M'), Some('X'), + _, _, _, _, + _, _, _, _, + _, _, _, _ + ) = map { + directions.push(Direction::West); + } + + if let ( + Some('X'), _, _, _, + Some('M'), _, _, _, + Some('A'), _, _, _, + Some('S'), _, _, _ + ) = map { + directions.push(Direction::South); + } + + if let ( + Some('S'), _, _, _, + Some('A'), _, _, _, + Some('M'), _, _, _, + Some('X'), _, _, _ + ) = map { + directions.push(Direction::North); + } + + if let ( + Some('X'), _, _, _, + _, Some('M'), _, _, + _, _, Some('A'), _, + _, _, _, Some('S') + ) = map { + directions.push(Direction::SouthEast); + } + + if let ( + Some('S'), _, _, _, + _, Some('A'), _, _, + _, _, Some('M'), _, + _, _, _, Some('X') + ) = map { + directions.push(Direction::NorthWest); + } + + if let ( + _, _, _, Some('X'), + _, _, Some('M'), _, + _, Some('A'), _, _, + Some('S'), _, _, _ + ) = map { + directions.push(Direction::SouthWest); + } + + if let ( + _, _, _, Some('S'), + _, _, Some('A'), _, + _, Some('M'), _, _, + Some('X'), _, _, _ + ) = map { + directions.push(Direction::SouthEast); + } + + directions } 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) { - dprintln!("FOUND AT {} {} '{}' - DIR: {:?}", x, y, self.0[y][x], _dir); - count += 1; - } + count += self.get_xmas_directions(x, y).len(); } } - return count; + return count as u64; } } From 2d89799e442fecb805735b9f5cc3d7569221cb01 Mon Sep 17 00:00:00 2001 From: LeMoonStar Date: Wed, 4 Dec 2024 19:38:35 +0100 Subject: [PATCH 2/3] =?UTF-8?q?=E2=9C=A8=20Day=204:=20Solved=20part=202!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++-- src/days/d04.rs | 49 ++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 5b0b40b..c42bf21 100644 --- a/README.md +++ b/README.md @@ -3,8 +3,8 @@ [![About](https://img.shields.io/badge/Advent%20of%20Code-2024-brightgreen?style=flat-square)](https://adventofcode.com/2024/about) [![Language: Rust](https://img.shields.io/badge/Language-Rust-orange.svg?style=flat-square)](https://en.wikipedia.org/wiki/Rust_(programming_language)) [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square)](https://mit-license.org/) -![Days completed](https://img.shields.io/badge/Days%20completed-2.5-red?style=flat-square) -![Stars](https://img.shields.io/badge/Stars-5-yellow?style=flat-square) +![Days completed](https://img.shields.io/badge/Days%20completed-3.5-red?style=flat-square) +![Stars](https://img.shields.io/badge/Stars-7-yellow?style=flat-square) > ⚠️ This README is copied from my previous years solution. It is not fully adopted to 2024 yet. diff --git a/src/days/d04.rs b/src/days/d04.rs index 564ed4b..d69ce05 100644 --- a/src/days/d04.rs +++ b/src/days/d04.rs @@ -116,6 +116,38 @@ impl LetterWall { directions } + #[rustfmt::skip] + fn is_cross_mas(&self, x: usize, y: usize) -> bool { + match( + self.at( x, y), self.at( x+1, y), self.at( x+2, y), + self.at( x, y+1), self.at( x+1, y+1), self.at( x+2, y+1), + self.at( x, y+2), self.at( x+1, y+2), self.at( x+2, y+2) + ) { + ( + Some('M'), _,Some('S'), + _, Some('A'), _, + Some('M'), _, Some('S'), + ) => true, + ( + Some('S'), _,Some('S'), + _, Some('A'), _, + Some('M'), _, Some('M'), + ) => true, + ( + Some('M'), _,Some('M'), + _, Some('A'), _, + Some('S'), _, Some('S'), + ) => true, + ( + Some('S'), _,Some('M'), + _, Some('A'), _, + Some('S'), _, Some('M'), + ) => true, + _ => false + } + } + + pub fn count_xmas(&self) -> u64 { let mut count = 0; for y in 0..self.0.len() { @@ -125,6 +157,18 @@ impl LetterWall { } return count as u64; } + + pub fn count_cross_mas(&self) -> u64 { + let mut count = 0; + for y in 0..self.0.len() { + for x in 0..self.0[0].len() { + if self.is_cross_mas(x, y) { + count += 1; + } + } + } + return count as u64; + } } impl From<&str> for LetterWall { @@ -141,7 +185,7 @@ impl DayImpl for Day { } fn expected_results() -> (Answer, Answer) { - (Answer::Number(18), Answer::Number(0)) + (Answer::Number(18), Answer::Number(9)) } fn init(input: &str) -> (Self, Data) { @@ -149,11 +193,10 @@ impl DayImpl for Day { } fn one(&self, data: &mut Data) -> Answer { - // NOTE TO SELF: Actual result above 2334 Answer::Number(data.count_xmas()) } fn two(&self, data: &mut Data) -> Answer { - Answer::Number(0) + Answer::Number(data.count_cross_mas()) } } From d25a9aad57f175c780b9ab5627ee24bf90170ea1 Mon Sep 17 00:00:00 2001 From: LeMoonStar Date: Wed, 4 Dec 2024 19:45:57 +0100 Subject: [PATCH 3/3] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20Day=204:=20Performance?= =?UTF-8?q?=20and=20cleanup?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/days/d04.rs | 60 +++++++++++++++++++------------------------------ 1 file changed, 23 insertions(+), 37 deletions(-) diff --git a/src/days/d04.rs b/src/days/d04.rs index d69ce05..786039f 100644 --- a/src/days/d04.rs +++ b/src/days/d04.rs @@ -1,21 +1,7 @@ -use crate::dprintln; - use super::{Answer, Day, DayImpl}; const CURRENT_DAY: u8 = 4; -#[derive(Debug, Clone, Copy)] -enum Direction { - East, - West, - North, - South, - NorthEast, - NorthWest, - SouthEast, - SouthWest, -} - #[derive(Debug, Clone)] pub struct LetterWall(Vec>); @@ -31,8 +17,8 @@ impl LetterWall { } #[rustfmt::skip] - fn get_xmas_directions(&self, x: usize, y: usize) -> Vec { - let mut directions = vec!(); + fn get_xmas_direction_count(&self, x: usize, y: usize) -> u64 { + let mut directions = 0; let map = ( self.at(x, y), self.at(x+1, y), self.at(x+2, y), self.at(x+3, y), @@ -47,7 +33,7 @@ impl LetterWall { _, _, _, _, _, _, _, _ ) = map { - directions.push(Direction::East); + directions += 1; } if let ( @@ -56,7 +42,7 @@ impl LetterWall { _, _, _, _, _, _, _, _ ) = map { - directions.push(Direction::West); + directions += 1; } if let ( @@ -65,7 +51,7 @@ impl LetterWall { Some('A'), _, _, _, Some('S'), _, _, _ ) = map { - directions.push(Direction::South); + directions += 1; } if let ( @@ -74,7 +60,7 @@ impl LetterWall { Some('M'), _, _, _, Some('X'), _, _, _ ) = map { - directions.push(Direction::North); + directions += 1; } if let ( @@ -83,7 +69,7 @@ impl LetterWall { _, _, Some('A'), _, _, _, _, Some('S') ) = map { - directions.push(Direction::SouthEast); + directions += 1; } if let ( @@ -92,7 +78,7 @@ impl LetterWall { _, _, Some('M'), _, _, _, _, Some('X') ) = map { - directions.push(Direction::NorthWest); + directions += 1; } if let ( @@ -101,7 +87,7 @@ impl LetterWall { _, Some('A'), _, _, Some('S'), _, _, _ ) = map { - directions.push(Direction::SouthWest); + directions += 1; } if let ( @@ -110,7 +96,7 @@ impl LetterWall { _, Some('M'), _, _, Some('X'), _, _, _ ) = map { - directions.push(Direction::SouthEast); + directions += 1; } directions @@ -124,24 +110,24 @@ impl LetterWall { self.at( x, y+2), self.at( x+1, y+2), self.at( x+2, y+2) ) { ( - Some('M'), _,Some('S'), - _, Some('A'), _, - Some('M'), _, Some('S'), + Some('M'), _, Some('S'), + _, Some('A'), _, + Some('M'), _, Some('S'), ) => true, ( - Some('S'), _,Some('S'), - _, Some('A'), _, - Some('M'), _, Some('M'), + Some('S'), _, Some('S'), + _, Some('A'), _, + Some('M'), _, Some('M'), ) => true, ( - Some('M'), _,Some('M'), - _, Some('A'), _, - Some('S'), _, Some('S'), + Some('M'), _, Some('M'), + _, Some('A'), _, + Some('S'), _, Some('S'), ) => true, ( - Some('S'), _,Some('M'), - _, Some('A'), _, - Some('S'), _, Some('M'), + Some('S'), _, Some('M'), + _, Some('A'), _, + Some('S'), _, Some('M'), ) => true, _ => false } @@ -152,7 +138,7 @@ impl LetterWall { let mut count = 0; for y in 0..self.0.len() { for x in 0..self.0[0].len() { - count += self.get_xmas_directions(x, y).len(); + count += self.get_xmas_direction_count(x, y); } } return count as u64;