aboutsummaryrefslogtreecommitdiff
path: root/2020/23-Crab_Cups
diff options
context:
space:
mode:
authorJulien Dessaux2023-05-16 00:45:07 +0200
committerJulien Dessaux2023-05-16 00:45:07 +0200
commitfb5cbdb4d202c5d5bfc176a195f5d4696340621a (patch)
tree8eb2255538d50bf65979a920b5f588c38087c882 /2020/23-Crab_Cups
parent2020-23 part 1 in haskell (diff)
downloadadvent-of-code-fb5cbdb4d202c5d5bfc176a195f5d4696340621a.tar.gz
advent-of-code-fb5cbdb4d202c5d5bfc176a195f5d4696340621a.tar.bz2
advent-of-code-fb5cbdb4d202c5d5bfc176a195f5d4696340621a.zip
2020-23 part 2 in haskell
Diffstat (limited to '2020/23-Crab_Cups')
-rw-r--r--2020/23-Crab_Cups/first.hs1
-rw-r--r--2020/23-Crab_Cups/second.hs72
2 files changed, 72 insertions, 1 deletions
diff --git a/2020/23-Crab_Cups/first.hs b/2020/23-Crab_Cups/first.hs
index 1b3e900..a338e69 100644
--- a/2020/23-Crab_Cups/first.hs
+++ b/2020/23-Crab_Cups/first.hs
@@ -14,7 +14,6 @@ exampleExpectedOutput = 67384529
type Cup = Int
type Input = [Cup]
-type History = [Input]
type Parser = Parsec Void String
diff --git a/2020/23-Crab_Cups/second.hs b/2020/23-Crab_Cups/second.hs
new file mode 100644
index 0000000..d2ffa5f
--- /dev/null
+++ b/2020/23-Crab_Cups/second.hs
@@ -0,0 +1,72 @@
+-- requires cabal install --lib megaparsec parser-combinators
+module Main (main) where
+import Control.Monad (void, when)
+import Data.List qualified as L
+import Data.Map qualified as M
+import Data.Maybe (fromJust)
+import Data.Set qualified as S
+import Data.Void (Void)
+import Text.Megaparsec
+import Text.Megaparsec.Char
+import System.Exit (die)
+
+exampleExpectedOutput = 149245887792
+
+type Cup = Int
+type Input = [Cup]
+type Input' = M.Map Cup Cup
+
+type Parser = Parsec Void String
+
+parseCup :: Parser Cup
+parseCup = do
+ n <- digitChar
+ return $ read [n] - 1 -- so that there is a 0 in there and then we can use modulo
+
+parseInput' :: Parser Input
+parseInput' = some parseCup <* optional (char '\n') <* eof
+
+parseInput :: String -> IO Input
+parseInput filename = do
+ input <- readFile filename
+ case runParser parseInput' filename input of
+ Left bundle -> die $ errorBundlePretty bundle
+ Right input' -> return input'
+
+score :: Input' -> Int
+score input = (one + 1) * (two + 1)
+ where
+ one = input M.! 0
+ two = input M.! one
+
+compute' :: Cup -> Input' -> Int -> Int
+compute' _ input 0 = score input
+compute' current input remainingMoves = compute' next step (remainingMoves - 1)
+ where
+ one = input M.! current
+ two = input M.! one
+ three = input M.! two
+ next = input M.! three
+ cups = [one, two, three]
+ mone = (current - 1) `mod` 1_000_000
+ mtwo = (current - 2) `mod` 1_000_000
+ mthree = (current - 3) `mod` 1_000_000
+ mfour = (current - 4) `mod` 1_000_000
+ destination :: Int
+ destination = if L.elem mone cups then (if L.elem mtwo cups then (if L.elem mthree cups then mfour else mthree) else mtwo) else mone
+ afterDestination = input M.! destination
+ step :: Input'
+ step = M.insert current next $ M.insert destination one $ M.insert three afterDestination input
+
+compute :: Input -> Int
+compute input = compute' (head input) input' 10_000_000
+ where
+ input' = M.fromList (zip input (tail input) ++ [(last input, 9)] ++ (zip [9..999_998] [10..999_999]) ++ [(999_999, head input)])
+
+main :: IO ()
+main = do
+ example <- parseInput "example"
+ let exampleOutput = compute example
+ when (exampleOutput /= exampleExpectedOutput) (die $ "example failed: got " ++ show exampleOutput ++ " instead of " ++ show exampleExpectedOutput)
+ input <- parseInput "input"
+ print $ compute input