X-Git-Url: https://git.njae.me.uk/?p=advent-of-code-20.git;a=blobdiff_plain;f=advent25%2Fsrc%2Fadvent25.hs;fp=advent25%2Fsrc%2Fadvent25.hs;h=548f71ee2f623ed9384c61c27ce906649667d8ef;hp=0000000000000000000000000000000000000000;hb=6cf726976e228ca8cf2aa4e2b9458add2cc62416;hpb=d51325ae0b835dd8928bd136d625db22741b4e97 diff --git a/advent25/src/advent25.hs b/advent25/src/advent25.hs new file mode 100644 index 0000000..548f71e --- /dev/null +++ b/advent25/src/advent25.hs @@ -0,0 +1,43 @@ +-- import Debug.Trace + +-- import Math.NumberTheory.Moduli.DiscreteLogarithm +-- import Data.Finite (Finite, modulo, getFinite) +-- import GHC.TypeNats (KnownNat) +-- import Data.Maybe + +import Data.Semigroup + +newtype MExp = MExp Integer + deriving (Show, Eq) + +instance Semigroup MExp where + (MExp a) <> (MExp b) = MExp $ (a * b) `mod` modularBase + + +subject :: MExp +subject = MExp 7 + +modularBase :: Integer +modularBase = 20201227 + +main :: IO () +main = + do text <- readFile "data/advent25.txt" + let [cardKey, doorKey] = map read $ lines text + print $ part1 cardKey doorKey + +part1 cardKey doorKey = publicKey + where cardRounds = findRounds cardKey + MExp publicKey = stimes cardRounds (MExp doorKey) + +findRounds :: Integer -> Integer +findRounds target = fst $ until (foundRounds t) countingExponential (0, seed) + where t = MExp target + seed = MExp 1 + +foundRounds :: MExp -> (Integer, MExp) -> Bool +foundRounds target (_n, v) = v == target + +countingExponential :: (Integer, MExp) -> (Integer, MExp) +countingExponential (n, v) = (n + 1, v <> subject) +