From 883e5fb8da899d92a1277aa241f1f224eb807390 Mon Sep 17 00:00:00 2001 From: JasterV <49537445+JasterV@users.noreply.github.com> Date: Tue, 25 Mar 2025 21:41:06 +0100 Subject: [PATCH] day4 partTwo WIP --- src/Day4.hs | 6 +++++- src/Day4/Matrix.hs | 21 ++++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/Day4.hs b/src/Day4.hs index f3edc12..b9cf406 100644 --- a/src/Day4.hs +++ b/src/Day4.hs @@ -20,4 +20,8 @@ partOne input = countXMAS [] = 0 partTwo :: String -> Int -partTwo _input = 0 +partTwo input = + let matrix = M.buildMatrix (lines input) + in length $ filter (isXMAS . fst) $ filter ((== 'a') . snd) $ M.toList matrix + where + isXMAS = undefined diff --git a/src/Day4/Matrix.hs b/src/Day4/Matrix.hs index 21dfe35..7ab0dd7 100644 --- a/src/Day4/Matrix.hs +++ b/src/Day4/Matrix.hs @@ -1,10 +1,20 @@ {-# LANGUAGE LambdaCase #-} {-# LANGUAGE ScopedTypeVariables #-} -module Day4.Matrix (Matrix, buildMatrix, groupWith) where +module Day4.Matrix + ( Matrix, + buildMatrix, + groupWith, + getValue, + getValues, + toList, + ) +where +import Data.Foldable (find) import Data.IntMap.Lazy (IntMap) import qualified Data.IntMap.Lazy as IntMap +import Data.Maybe type AssocList k v = [(k, v)] @@ -23,6 +33,15 @@ buildMatrix xs = Matrix (go xs 0 []) let acc' = ((row, column), x) : acc in parseRow xs' (row, column + 1) acc' +toList :: Matrix v -> [((Int, Int), v)] +toList (Matrix assoc) = assoc + +getValue :: (Int, Int) -> Matrix v -> Maybe v +getValue position (Matrix assoc) = snd <$> find ((== position) . fst) assoc + +getValues :: [(Int, Int)] -> Matrix v -> [v] +getValues positions matrix = mapMaybe (`getValue` matrix) positions + {-- Given a matrix of elements and a function mapping a position into an aggregation of its values, group the elements by the aggregation result