From 17ae56a271ad503336dcb4f4fb9f0773c5cb3359 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Mon, 15 May 2023 00:36:14 +0200 Subject: 2020-23 part 1 in haskell --- 2020/23-Crab_Cups/example | 1 + 2020/23-Crab_Cups/first.hs | 66 ++++++++++++++++++++++++++++++++++++++++++++++ 2020/23-Crab_Cups/input | 1 + 3 files changed, 68 insertions(+) create mode 100644 2020/23-Crab_Cups/example create mode 100644 2020/23-Crab_Cups/first.hs create mode 100644 2020/23-Crab_Cups/input diff --git a/2020/23-Crab_Cups/example b/2020/23-Crab_Cups/example new file mode 100644 index 0000000..ab40847 --- /dev/null +++ b/2020/23-Crab_Cups/example @@ -0,0 +1 @@ +389125467 diff --git a/2020/23-Crab_Cups/first.hs b/2020/23-Crab_Cups/first.hs new file mode 100644 index 0000000..1b3e900 --- /dev/null +++ b/2020/23-Crab_Cups/first.hs @@ -0,0 +1,66 @@ +-- 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 = 67384529 + +type Cup = Int +type Input = [Cup] +type History = [Input] + +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 (0:xs) = read . concat $ map (\i -> show (i+1)) xs +score (x:xs) = score $ xs ++ [x] + +compute' :: Input -> Int -> Int +compute' input 0 = score input +compute' (current:one:two:three:xs) remainingMoves = compute' step (remainingMoves - 1) + where + l = length xs + 4 + destinationIndex :: Int + destinationIndex = case L.elemIndex ((current - 1) `mod` l) xs of + Just i -> i + Nothing -> case L.elemIndex ((current - 2) `mod` l) xs of + Just i -> i + Nothing -> case L.elemIndex ((current - 3) `mod` l) xs of + Just i -> i + Nothing -> fromJust $ L.elemIndex ((current - 4) `mod` l) xs + destinationIndex' = destinationIndex + 1 + step :: Input + step = concat [take destinationIndex' xs, [one, two, three], drop destinationIndex' xs, [current]] + +compute :: Input -> Int +compute input = compute' input 100 + +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 diff --git a/2020/23-Crab_Cups/input b/2020/23-Crab_Cups/input new file mode 100644 index 0000000..f69e63f --- /dev/null +++ b/2020/23-Crab_Cups/input @@ -0,0 +1 @@ +963275481 -- cgit v1.2.3