Last active
December 15, 2020 22:43
-
-
Save fxn/be211ef18b9cfd0c05c143e1215e0321 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# 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