From 2d89799e442fecb805735b9f5cc3d7569221cb01 Mon Sep 17 00:00:00 2001 From: LeMoonStar Date: Wed, 4 Dec 2024 19:38:35 +0100 Subject: [PATCH] =?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()) } }