aoc/app/4.hs
2024-12-04 10:51:02 +01:00

45 lines
1.3 KiB
Haskell

{-# LANGUAGE LambdaCase #-}
module Main where
import Data.Functor
import Data.List
import Data.Universe.Helpers
import Text.Regex.PCRE
import Data.Function
import Debug.Trace
parse1 :: String -> [String]
parse1 a =
let x = lines a
x' = x <&> reverse in
x ++ -- vertical ⇒
x' ++ -- vertical ⇐
transpose x ++ -- horizontal ⇓
(transpose x <&> reverse) ++ -- horizontal ⇓
diagonals x ++ -- diagonal ⇗
diagonals x' ++ -- diagonal ⇖
(diagonals x <&> reverse) ++ -- diagonal ⇙
(diagonals x'<&> reverse) -- diagonal ⇘
solve1 :: [String] -> Int
solve1 = sum . (length . (getAllTextMatches . (=~ "XMAS") :: String -> [String]) <$>)
pattern2 :: Int -> String
pattern2 i = let i' = i-2 in
"M.M.{"++ show i' ++"}.A..{" ++ show i' ++ "}S.S|" ++
"M.S.{"++ show i' ++"}.A..{" ++ show i' ++ "}M.S|" ++
"S.M.{"++ show i' ++"}.A..{" ++ show i' ++ "}S.M|" ++
"S.S.{"++ show i' ++"}.A..{" ++ show i' ++ "}M.M"
solve2 :: String -> Int
solve2 s =
let l = length . head . lines $ s in
let t = (getAllTextSubmatches . (=~ pattern2 l) :: String -> [String]) . intercalate "U" . lines <$> tails s
t' = nub . concat $ t in
length t'
main :: IO ()
main = readFile "inputs/4" >>= \i ->
print (solve1 $ parse1 i) >>
print (solve2 i)