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 | |
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
-rw-r--r-- | 2024/04-Ceres_Search/example | 10 | ||||
-rw-r--r-- | 2024/04-Ceres_Search/first.hs | 51 | ||||
-rw-r--r-- | 2024/04-Ceres_Search/input | 140 | ||||
-rw-r--r-- | 2024/04-Ceres_Search/second.hs | 56 |
4 files changed, 257 insertions, 0 deletions
diff --git a/2024/04-Ceres_Search/example b/2024/04-Ceres_Search/example new file mode 100644 index 0000000..1f4eda2 --- /dev/null +++ b/2024/04-Ceres_Search/example @@ -0,0 +1,10 @@ +MMMSXXMASM +MSAMXMSMSA +AMXSXMAAMM +MSAMASMSMX +XMASAMXAMM +XXAMMXXAMA +SMSMSASXSS +SAXAMASAAA +MAMMMXMMMM +MXMXAXMASX diff --git a/2024/04-Ceres_Search/first.hs b/2024/04-Ceres_Search/first.hs new file mode 100644 index 0000000..673a45f --- /dev/null +++ b/2024/04-Ceres_Search/first.hs @@ -0,0 +1,51 @@ +-- requires cabal install --lib megaparsec parser-combinators heap vector +module Main (main) where + +import Control.Monad (void, when) +import qualified Data.List as L +import Data.Void (Void) +import Text.Megaparsec +import Text.Megaparsec.Char + +exampleExpectedOutput = 18 + +type Line = [Char] +type Input = [Line] + +type Parser = Parsec Void String + +parseInput' :: Parser Input +parseInput' = some (some letterChar <* eol) <* eof + +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' + +rotate90 = reverse . L.transpose +rotate180 = rotate90 . rotate90 + +diagonals = (++) <$> L.transpose . zipWith drop [0..] + <*> L.transpose . zipWith drop [1..] . rotate180 + +countInString :: String -> Int +countInString [] = 0 +countInString s@('X':'M':'A':'S':_) = 1 + (countInString $ tail s) +countInString s@('S':'A':'M':'X':_) = 1 + (countInString $ tail s) +countInString s = countInString $ tail s + +compute :: Input -> Int +compute lines = sum $ map countInString combinations + where + columns = rotate90 lines + combinations = lines ++ columns ++ diagonals lines ++ diagonals columns + +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 diff --git a/2024/04-Ceres_Search/input b/2024/04-Ceres_Search/input new file mode 100644 index 0000000..e7dbfac --- /dev/null +++ b/2024/04-Ceres_Search/input @@ -0,0 +1,140 @@ +MSSMMSXSMSMSXMXXXMSMSSMSAMXAXASXSASMSMXMXMMAMSMMMSMSXSAMXSSXMASMMMXXMASXMSXMSSXXXAMMXMXSMSAMAAASAMXMMSSSXXSMMXXXSMXSXMXMMXXMASMMMMSXXMAXXMAS +XAAAAAXMASMSASXMAMAAMXAAMSSXSAMASASAAAXSAMXAMAAASAASAMMSMMMAXAXAMSMMAAAAAXASXAASXSSMAMAMXMASXMMAASASXMAMAMMAMSMMMMAAAMMSMXSAMXSAMASMSMSSMSSM +MMSMMMSMAMAMAMSAMXMSMMXMAAXAMAMAMMMXMSMSAMSASMSMSMMMAMAAMAMSMMSXMAAMMSSMMSXMMMMAAAXMAMAMMSXMAXXXXSASXMASAASAMXMAAMSSXMXAAAMASAMXMXSAAAAXAXXS +AXXXSAMMAMAMMMSXMASAMXMXMMMXXAMXSXAXXAAMAMSMMAAXSXXXXMSSSSSXAASMSMSMAAXAASAMXSSMMMXMXXASAMAMAMSSMMXMASAXMXXXSASMSXAAMSSMSMSAMASXSMSXMSMMSMMM +SSM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MAMAXAMMMMSMAMAAXAMAMXAMMSS +SASXAAXAAXAMXSXMSMMMSMAAAAMXMXMSMSAMXASMMMSSXMXMAXXAAAXAMMMSAMXSXSASMXMASMXMMASAMSMSSSMXMMMASAXMXSMMAMAMSASAMXSMMSSMMMMXAAMMMMAMXXMMSSMSMSAX +MMMXMSMMMSMMAMAAXXAAAMSMSMSMMSMSAMAMMMXAXAXMXSAMXSSMMMSMMSXMAMXMASMMMAMAMMMMMAMAMXAXMAMASASAMXMMAAXSSMAMSXSASAAXAAAAXAXSSXSAAMXSSMSAASAMXMMS +SAAXSXAXAAAAXMMMAMSSMMAAAMAAMXAMXSAMXXSAMXSAAXXXMAXXAXAXAXAAXMAMAMAAMAMMMSAMMXSSMMXMMAMAAAMSMAMXAMMXXXXMMAMAAXXMMSSMMSMMMAMXMSAXAAMMMMMMXAAM +SMASXSMMSSSMSXSMSMXAXSMSMSSSMMXMASASMASAMAXMASMMSMMSSSMMSSXMASAMASXMSSSMAMSASMMAAMMSSSSXMAMAMMMSAMAMMMMAMAMMMASAMAXAAAAAMMMSXMASMSMMASAMSMSS +SAMXXXXAAMAMMAXAXMASMXXAAMXMASXMXSAMMAMAMMSAMSAASAAAXAXAMSASASASMSAAAAAMXSMMMASMMSAAMXMXXSSMSAASAMSXMASASASAXMASMMSMMSSMSXXSAMXXXAXSASAMMAAM +SSXMASMMSSMMMAMMMASAMXSMSMASAMAXAMAMMSSXMXXXMSXMSMMXSAMSSSXMMSAMXSMMMSMMSMMXAXXXSMMXSAXSAMXXMMXSSMASMXSMSMSASMXXMXXXAXMASAXSXMSASAMMXSXMMMMS 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 |