diff options
author | Julien Dessaux | 2024-12-04 11:32:17 +0100 |
---|---|---|
committer | Julien Dessaux | 2024-12-04 11:32:17 +0100 |
commit | 4dee621ccf74908b4753ca54c13452d6b739a7f5 (patch) | |
tree | 13589fc43ad72c4782512abcd6e9c8f9266e4c0f /2024/04-Ceres_Search/second.hs | |
parent | 2024-03 in haskell (diff) | |
download | advent-of-code-4dee621ccf74908b4753ca54c13452d6b739a7f5.tar.gz advent-of-code-4dee621ccf74908b4753ca54c13452d6b739a7f5.tar.bz2 advent-of-code-4dee621ccf74908b4753ca54c13452d6b739a7f5.zip |
2024-04 in haskell
Diffstat (limited to '2024/04-Ceres_Search/second.hs')
-rw-r--r-- | 2024/04-Ceres_Search/second.hs | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/2024/04-Ceres_Search/second.hs b/2024/04-Ceres_Search/second.hs new file mode 100644 index 0000000..aa8008d --- /dev/null +++ b/2024/04-Ceres_Search/second.hs @@ -0,0 +1,56 @@ +-- requires cabal install --lib megaparsec parser-combinators heap vector +module Main (main) where + +import Control.Monad (void, when) +import qualified Data.Vector as V +import qualified Data.Vector.Unboxed as VU +import Data.Void (Void) +import Text.Megaparsec +import Text.Megaparsec.Char + +exampleExpectedOutput = 9 + +type Line = VU.Vector Char +type Input = V.Vector Line + +type Parser = Parsec Void String + +parseLine :: Parser Line +parseLine = do + line <- some letterChar <* eol + return $ VU.generate (length line) (line !!) + +parseInput' :: Parser Input +parseInput' = do + line <- some parseLine <* eof + return $ V.generate (length line) (line !!) + +parseInput :: String -> IO Input +parseInput filename = do + input <- readFile filename + case runParser parseInput' filename input of + Left bundle -> error $ errorBundlePretty bundle + Right input' -> return input' + +compute :: Input -> Int +compute g = length $ filter xmas as + where + l = V.length g + as = [(x, y) | x<-[1..l-2], y<-[1..l-2], g V.! x VU.! y == 'A'] + xmas :: (Int, Int) -> Bool + xmas (x, y) = d1 && d2 + where + tl = g V.! (x-1) VU.! (y-1) + tr = g V.! (x+1) VU.! (y-1) + bl = g V.! (x-1) VU.! (y+1) + br = g V.! (x+1) VU.! (y+1) + d1 = tl == 'M' && br == 'S' || tl == 'S' && br == 'M' + d2 = bl == 'M' && tr == 'S' || bl == 'S' && tr == 'M' + +main :: IO () +main = do + example <- parseInput "example" + let exampleOutput = compute example + when (exampleOutput /= exampleExpectedOutput) (error $ "example failed: got " ++ show exampleOutput ++ " instead of " ++ show exampleExpectedOutput) + input <- parseInput "input" + print $ compute input |