From ed5ac4446c07157fec1cb93cf572601a15c1be38 Mon Sep 17 00:00:00 2001 From: Rh4096 Date: Wed, 4 Dec 2024 20:07:33 +0700 Subject: [PATCH] day 4 --- src/lib.rs | 1 + src/main.rs | 4 +- src/solutions/day_4.rs | 104 +++++++++++++++++++++++++++++++++++++++++ src/utils/mod.rs | 1 + 4 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 src/utils/mod.rs diff --git a/src/lib.rs b/src/lib.rs index 3242762..d952180 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1 +1,2 @@ pub mod solutions; +pub mod utils; diff --git a/src/main.rs b/src/main.rs index ba7a4b2..61f92a8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ -use aoc2024::solutions::{day_3::AocDayThreeSolution, AocSolution}; +use aoc2024::solutions::{day_4::AocDayFourSolution, AocSolution}; fn main() { - let sol = AocDayThreeSolution; + let sol = AocDayFourSolution; println!("p1: {}\np2: {}", sol.part_one(), sol.part_two()) } diff --git a/src/solutions/day_4.rs b/src/solutions/day_4.rs index e69de29..d973ee5 100644 --- a/src/solutions/day_4.rs +++ b/src/solutions/day_4.rs @@ -0,0 +1,104 @@ +use std::collections::HashMap; + +use super::AocSolution; + +const DIRS: [(i16, i16); 8] = [ + (1, 0), + (1, -1), + (0, -1), + (-1, -1), + (-1, 0), + (-1, 1), + (0, 1), + (1, 1), +]; + +struct XmasGrid { + pub grid: HashMap<(i16, i16), char>, +} + +impl XmasGrid { + fn new(src: &str) -> Self { + Self { + grid: src + .lines() + .zip(0..) + .flat_map(|(l, y)| l.chars().zip(0..).map(move |(c, x)| ((x, y), c))) + .collect(), + } + } + + fn find_xmas(&self, pos: (i16, i16)) -> usize { + let mut tmp = 0; + for (dx, dy) in DIRS { + let mut found = true; + let (mut x, mut y) = pos; + + for nxt in ['M', 'A', 'S'] { + x += dx; + y += dy; + + if self.grid.get(&(x, y)) != Some(&nxt) { + found = false; + break; + } + } + + if found { + tmp += 1; + } + } + + tmp + } + + fn find_x_mas(&self, pos: (i16, i16)) -> usize { + let (x, y) = pos; + + let tr = self.grid.get(&(x + 1, y + 1)); + let br = self.grid.get(&(x + 1, y - 1)); + let bl = self.grid.get(&(x - 1, y - 1)); + let tl = self.grid.get(&(x - 1, y + 1)); + + match (tr, bl, tl, br) { + (Some('S'), Some('M'), Some('M'), Some('S')) => 1, + (Some('M'), Some('S'), Some('S'), Some('M')) => 1, + (Some('S'), Some('M'), Some('S'), Some('M')) => 1, + (Some('M'), Some('S'), Some('M'), Some('S')) => 1, + _ => 0, + } + } +} + +pub struct AocDayFourSolution; + +impl AocSolution for AocDayFourSolution { + type Output = usize; + const INPUT: &str = include_str!("../../input/day4.txt"); + + fn part_one(&self) -> Self::Output { + let xmas = XmasGrid::new(Self::INPUT); + let mut tmp = 0; + + for (&pos, &ch) in &xmas.grid { + if ch == 'X' { + tmp += xmas.find_xmas(pos) + } + } + + tmp as Self::Output + } + + fn part_two(&self) -> Self::Output { + let xmas = XmasGrid::new(Self::INPUT); + let mut tmp = 0; + + for (&pos, &ch) in &xmas.grid { + if ch == 'A' { + tmp += xmas.find_x_mas(pos) + } + } + + tmp as Self::Output + } +} diff --git a/src/utils/mod.rs b/src/utils/mod.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/utils/mod.rs @@ -0,0 +1 @@ +