-
-
Save mjrosenb/11552f705f504ed772187b90462c0ebd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.List | |
import qualified Data.IntMap.Strict as M | |
data GameState = GS Int Int (M.IntMap Int) deriving Show | |
parse:: String -> [Int] | |
parse [] = [] | |
parse l = | |
case reads l of | |
(z,',':rest):_ -> z:parse rest | |
(z,_):_ -> [z] | |
x -> error ("not handled: " ++ show (l, x)) | |
next (GS !n !prev !posns) = | |
case M.lookup prev posns of | |
Nothing -> 0 | |
Just x -> n-x | |
play n (gs@(GS !n' !prev !posns)) | |
| n == n' = gs | |
| otherwise = | |
let new = next gs | |
posns' = M.insert prev n' posns | |
in play n (GS (n'+1) new posns') | |
main = do | |
raw <- getContents | |
let nums = (parse raw) | |
gs = GS (length nums) (last nums) (M.fromList (zip (init nums) [1..])) | |
GS n res m = play (30000000) gs | |
print res | |
print (M.size m) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment