diff options
author | Julien Dessaux | 2023-05-21 22:43:08 +0200 |
---|---|---|
committer | Julien Dessaux | 2023-05-21 22:43:08 +0200 |
commit | 73f06861ac112431924012b55debc60193a1498f (patch) | |
tree | b14d627e6b8d4ce11a0a394d9a9ead517db781df | |
parent | 2020-25 in haskell (diff) | |
download | advent-of-code-73f06861ac112431924012b55debc60193a1498f.tar.gz advent-of-code-73f06861ac112431924012b55debc60193a1498f.tar.bz2 advent-of-code-73f06861ac112431924012b55debc60193a1498f.zip |
2020-25 in haskell v2
-rw-r--r-- | 2020/25-Combo_Breaker/first.hs | 23 |
1 files changed, 12 insertions, 11 deletions
diff --git a/2020/25-Combo_Breaker/first.hs b/2020/25-Combo_Breaker/first.hs index 151c9da..7979d34 100644 --- a/2020/25-Combo_Breaker/first.hs +++ b/2020/25-Combo_Breaker/first.hs @@ -1,32 +1,34 @@ --- requires cabal install --lib megaparsec parser-combinators +{-# LANGUAGE DataKinds #-} +-- requires cabal install --lib megaparsec parser-combinators mod 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.Mod import Data.Set qualified as S import Data.Void (Void) -import Math.NumberTheory.Powers.Modular import Text.Megaparsec import Text.Megaparsec.Char import System.Exit (die) exampleExpectedOutput = 14897079 -type Input = (Integer, Integer) +type I = Mod 20201227 +type Input = (I, I) type Parser = Parsec Void String -parseInteger :: Parser Integer -parseInteger = do +parseInt :: Parser (I) +parseInt = do n <- some digitChar void $ optional (char '\n') return $ read n parseInput' :: Parser Input parseInput' = do - a <- parseInteger - b <- parseInteger + a <- parseInt + b <- parseInt void eof return (a, b) @@ -37,10 +39,10 @@ parseInput filename = do Left bundle -> die $ errorBundlePretty bundle Right input' -> return input' -transform :: Integer -> Int -> Integer -transform subjectNum loopSize = powMod subjectNum loopSize 20201227 +transform :: I -> Int -> I +transform subjectNum loopSize = subjectNum ^% loopSize -compute :: Input -> Integer +compute :: Input -> I compute (cardPubKey, doorPubKey) = encryptionKey where cardLoopSize = fromJust . L.elemIndex cardPubKey $ map (transform 7) [0..] @@ -52,6 +54,5 @@ main = do example <- parseInput "example" let exampleOutput = compute example when (exampleOutput /= exampleExpectedOutput) (die $ "example failed: got " ++ show exampleOutput ++ " instead of " ++ show exampleExpectedOutput) - print "OK" input <- parseInput "input" print $ compute input |