Day 14.
This commit is contained in:
parent
3d1cac066d
commit
e4ba4a6a86
|
@ -25,7 +25,7 @@ Now located in this repository's wiki.
|
||||||
| 11 | ~ 7 |
|
| 11 | ~ 7 |
|
||||||
| 12 | ~ 1.5 |
|
| 12 | ~ 1.5 |
|
||||||
| 13 | ~ 0.5 |
|
| 13 | ~ 0.5 |
|
||||||
| 14 | |
|
| 14 | 22.3 |
|
||||||
| 15 | |
|
| 15 | |
|
||||||
| 16 | |
|
| 16 | |
|
||||||
| 17 | |
|
| 17 | |
|
||||||
|
|
|
@ -30,4 +30,4 @@ import qualified Day24
|
||||||
import qualified Day25
|
import qualified Day25
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = Day13.main
|
main = Day14.main
|
||||||
|
|
32
src/Day14.hs
32
src/Day14.hs
|
@ -1,6 +1,34 @@
|
||||||
module Day14 (main) where
|
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 :: IO ()
|
||||||
main = do
|
main = do
|
||||||
input <- readFile "input/14.txt"
|
print part1
|
||||||
print input
|
print part2
|
Loading…
Reference in New Issue