Day 09.
This commit is contained in:
parent
e736f827ae
commit
7fe5f45a60
|
@ -15,7 +15,7 @@ To arrive at [⟨ortho|normal⟩](https://hilb.ert.space) as December comes.
|
||||||
| 05 | ~ 1 |
|
| 05 | ~ 1 |
|
||||||
| 06 | ~ 3 |
|
| 06 | ~ 3 |
|
||||||
| 07 | ~ 1 |
|
| 07 | ~ 1 |
|
||||||
| 08 | |
|
| 08 | ~ 6.5 |
|
||||||
| 09 | |
|
| 09 | |
|
||||||
| 10 | |
|
| 10 | |
|
||||||
| 11 | |
|
| 11 | |
|
||||||
|
|
|
@ -30,4 +30,4 @@ import qualified Day24
|
||||||
import qualified Day25
|
import qualified Day25
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = Day07.main
|
main = Day09.main
|
||||||
|
|
40
src/Day09.hs
40
src/Day09.hs
|
@ -1,6 +1,42 @@
|
||||||
|
{-# LANGUAGE BangPatterns #-}
|
||||||
|
|
||||||
module Day09 (main) where
|
module Day09 (main) where
|
||||||
|
|
||||||
|
import Data.IntMap (IntMap, empty, insertWith)
|
||||||
|
import Data.Sequence (Seq((:<|)), fromList, (><), (<|))
|
||||||
|
import qualified Data.Sequence as S (length, drop, take)
|
||||||
|
|
||||||
|
players = 411
|
||||||
|
lastMarble = 7105800
|
||||||
|
infix 5 %
|
||||||
|
(%) = mod
|
||||||
|
|
||||||
|
-- (scoreboard, marbles, marble, player)
|
||||||
|
type State = (Scoreboard, Marbles, Int, Int)
|
||||||
|
type Scoreboard = IntMap Int
|
||||||
|
type Marbles = Seq Int
|
||||||
|
|
||||||
|
-- pop from the front and push in the back n marbles
|
||||||
|
spin :: Int -> Marbles -> Marbles
|
||||||
|
spin n m = S.drop n m >< S.take n m
|
||||||
|
|
||||||
|
-- pop from the back and push in the front n marbles
|
||||||
|
unspin :: Int -> Marbles -> Marbles
|
||||||
|
unspin n m = S.drop (S.length m - n) m >< S.take (S.length m - n) m
|
||||||
|
|
||||||
|
part1 :: State -> Int
|
||||||
|
part1 (scores, marbles, m, p)
|
||||||
|
| nextMarble == lastMarble = maximum scores
|
||||||
|
| nextMarble % 23 == 0 =
|
||||||
|
let !(m:<|nextMarbles) = unspin 7 marbles
|
||||||
|
nextScores = insertWith (+) nextPlayer (nextMarble + m) scores
|
||||||
|
in part1 (nextScores, nextMarbles, nextMarble, nextPlayer)
|
||||||
|
| otherwise =
|
||||||
|
let !nextMarbles = nextMarble <| spin 2 marbles
|
||||||
|
in part1 (scores, nextMarbles, nextMarble, nextPlayer)
|
||||||
|
where nextMarble = m + 1
|
||||||
|
nextPlayer = (p % players) + 1
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
input <- readFile "input/09.txt"
|
print $ part1 (empty, fromList [1, 0], 1, 1)
|
||||||
print input
|
|
Loading…
Reference in New Issue