day4 partTwo WIP

This commit is contained in:
JasterV 2025-03-25 21:41:06 +01:00
commit 883e5fb8da
2 changed files with 25 additions and 2 deletions

View file

@ -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

View file

@ -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