diff --git a/README.md b/README.md index 0af7b19..0fd7332 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ Now located in this repository's wiki. | 11 | ~ 7 | | 12 | ~ 1.5 | | 13 | ~ 0.5 | -| 14 | | +| 14 | 22.3 | | 15 | | | 16 | | | 17 | | diff --git a/app/Main.hs b/app/Main.hs index 1e35e58..dd48e62 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -30,4 +30,4 @@ import qualified Day24 import qualified Day25 main :: IO () -main = Day13.main +main = Day14.main diff --git a/src/Day14.hs b/src/Day14.hs index 9457e44..d193121 100644 --- a/src/Day14.hs +++ b/src/Day14.hs @@ -1,6 +1,34 @@ module Day14 (main) where +import Data.Char (digitToInt, intToDigit) +import Data.List (tails, isPrefixOf) +import Data.Sequence (Seq(..), fromList, index, (><)) + +type Scores = Seq Int +type Elves = (Int, Int) -- indices of first and second elf + +(!) = index + +input = 793061 +inputList = [7, 9, 3, 0, 6, 1] +initialScores = [3, 7] +initialElves = (0, 1) + +getScores :: [Int] +getScores = initialScores ++ getNextScores initialElves (fromList initialScores) + where getNextScores (e1, e2) scores = + let newScores = map digitToInt . show $ scores ! e1 + scores ! e2 + nextScores = scores >< fromList newScores + nextElves = ((e1 + scores ! e1 + 1) `mod` length nextScores, (e2 + scores ! e2 + 1) `mod` length nextScores) + in newScores ++ getNextScores nextElves nextScores + +part1 :: String +part1 = map intToDigit . take 10 . drop input $ getScores + +part2 :: Int +part2 = length . takeWhile (not . (inputList `isPrefixOf`)) . tails $ getScores + main :: IO () main = do - input <- readFile "input/14.txt" - print input \ No newline at end of file + print part1 + print part2 \ No newline at end of file