aboutsummaryrefslogtreecommitdiff
path: root/2024/14-Restroom_Redoubt/second.hs
diff options
context:
space:
mode:
authorJulien Dessaux2024-12-18 00:01:57 +0100
committerJulien Dessaux2024-12-18 00:01:57 +0100
commitf879b793ec1875dcb927dbbf0e66912694ced82c (patch)
treea0e9fb99ad79df2ffa504ff11fef85404c78295f /2024/14-Restroom_Redoubt/second.hs
parent2024-03 part 2 in factor using an EBNF parser this time (diff)
downloadadvent-of-code-f879b793ec1875dcb927dbbf0e66912694ced82c.tar.gz
advent-of-code-f879b793ec1875dcb927dbbf0e66912694ced82c.tar.bz2
advent-of-code-f879b793ec1875dcb927dbbf0e66912694ced82c.zip
2024-14 in haskell
Diffstat (limited to '')
-rw-r--r--2024/14-Restroom_Redoubt/second.hs54
1 files changed, 54 insertions, 0 deletions
diff --git a/2024/14-Restroom_Redoubt/second.hs b/2024/14-Restroom_Redoubt/second.hs
new file mode 100644
index 0000000..cc23ce4
--- /dev/null
+++ b/2024/14-Restroom_Redoubt/second.hs
@@ -0,0 +1,54 @@
+-- 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
+
+type Pair = (Int, Int)
+type Robot = (Pair, Pair)
+type Input = [Robot]
+
+type Parser = Parsec Void String
+
+parseNumber :: Parser Int
+parseNumber = read <$> some (digitChar <|> char '-')
+
+parsePair :: Parser Pair
+parsePair = (,) <$> parseNumber <* char ','
+ <*> parseNumber
+
+parseRobot :: Parser Robot
+parseRobot = (,) <$> (string "p=" *> parsePair)
+ <*> (string " v=" *> parsePair)
+
+parseInput' :: Parser Input
+parseInput' = some (parseRobot <* 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'
+
+compute :: Int -> Int -> Input -> Int -- the minimum safety score is the answer
+compute width height input = fst $ L.foldl' (\acc@(_, a) n@(_, b) -> if a > b then n else acc) (0, 1000000000) safeties
+ where
+ safeties = [(i, safety i)|i<-[0..width*height]]
+ mx = width `div` 2
+ my = height `div` 2
+ safety i = product $ L.foldl' score [0, 0, 0, 0] $ map (step i) input
+ step i ((x, y), (v, w)) = (((x + v * i) `mod` width), ((y + w * i) `mod` height))
+ score acc@[a, b, c, d] (x, y) | x < mx && y < my = [a+1, b, c, d]
+ | x > mx && y < my = [a, b+1, c, d]
+ | x < mx && y > my = [a, b, c+1, d]
+ | x > mx && y > my = [a, b, c, d+1]
+ | otherwise = acc
+
+main :: IO ()
+main = do
+ input <- parseInput "input"
+ print $ compute 101 103 input