diff options
Diffstat (limited to '2024')
-rw-r--r-- | 2024/4.hs | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/2024/4.hs b/2024/4.hs new file mode 100644 index 0000000..913715f --- /dev/null +++ b/2024/4.hs @@ -0,0 +1,21 @@ +import Data.List (tails, transpose) + +main :: IO () +main = do + let a `startsWith` b = take (length b) a == b + let searchIn word lns = length (filter (`startsWith` word) (tails lns)) + length (filter (`startsWith` word) (tails (reverse lns))) + let rows = id + let cols = transpose + let unshift = zipWith drop [0..] + let maindiag = concatMap (take 1) . unshift + let upperdiags = transpose . unshift + let diags1 mat = map maindiag (tails (tail mat)) ++ upperdiags mat + let diags2 = diags1 . map reverse + input <- lines <$> getContents + print $ sum (map (searchIn "XMAS") (concatMap ($ input) [rows, cols, diags1, diags2])) + + let chunks n = filter ((== n) . length) . map (take n) . tails + let blocks n = concatMap (transpose . map (chunks n)) . chunks n + let rots blk = [blk, map reverse (transpose blk), reverse (map reverse blk), reverse (transpose blk)] + let isx_mas [[a,_,b],[_,c,_],[d,_,e]] = a == 'M' && b == 'M' && c == 'A' && d == 'S' && e == 'S' + print $ sum [fromEnum (isx_mas blk) | blk <- concatMap rots (blocks 3 input)] |