Skip to content

Instantly share code, notes, and snippets.

@mjrosenb
Created December 15, 2020 17:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjrosenb/11552f705f504ed772187b90462c0ebd to your computer and use it in GitHub Desktop.
Save mjrosenb/11552f705f504ed772187b90462c0ebd to your computer and use it in GitHub Desktop.
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