diff options
author | Tom Smeding <tom@tomsmeding.com> | 2024-12-04 16:21:37 +0100 |
---|---|---|
committer | Tom Smeding <tom@tomsmeding.com> | 2024-12-04 16:21:37 +0100 |
commit | 9fe655704e5dd35c988ea9bca34ed94f3a6ab8d2 (patch) | |
tree | f2ae53034d9ccbc5121bfe7e8f00e5d6da2acf8c /2024 | |
parent | 0f15c35338be0df27ade75527a080fd02efd8a61 (diff) |
4
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)] |