Skip to content

Instantly share code, notes, and snippets.

@fxn
Last active December 15, 2020 22:43
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 fxn/be211ef18b9cfd0c05c143e1215e0321 to your computer and use it in GitHub Desktop.
Save fxn/be211ef18b9cfd0c05c143e1215e0321 to your computer and use it in GitHub Desktop.
# Second edition of the solution using Table[int, int]. This is possible
# because we only care about a repetition of the last spoken number, I
# misunderstood an example as more generic than what it is, the rules of
# the game are clear about this.
import strutils, sequtils, tables
var input = "8,11,0,19,1,2".split(',').map(parseInt)
var turns: Table[int, int]
for i, n in input[0 .. ^2]:
turns[n] = i + 1
var mostRecentlySpoken = input[^1]
for turn in input.len .. 2019:
var next = 0
if mostRecentlySpoken in turns:
next = turn - turns[mostRecentlySpoken]
turns[mostRecentlySpoken] = turn
mostRecentlySpoken = next
echo mostRecentlySpoken
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment