1
0
Fork 0
This commit is contained in:
Jonathan Chan 2018-12-16 20:52:41 -08:00
parent 3d1cac066d
commit e4ba4a6a86
3 changed files with 32 additions and 4 deletions

View File

@ -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 | |

View File

@ -30,4 +30,4 @@ import qualified Day24
import qualified Day25
main :: IO ()
main = Day13.main
main = Day14.main

View File

@ -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
print part1
print part2