Skip to content

Instantly share code, notes, and snippets.

Created December 28, 2020 03:28
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
# read in aoc input
data = open("").read().strip().split(',')
def solve(match):
# add starting numbers to dict for tracking
pdict = {}
for i, p in enumerate(data):
pdict[int(p)] = [i + 1]
# setup tracking variables
last = int(data[-1])
turn = len(data)
while True:
# increase the turn each iteration
turn += 1
# exit if turn matches requested aoc number
if turn == match + 1:
# if num was only just spoken
if len(pdict[last]) == 1:
pdict[0] = [pdict[0][-1], turn]
last = 0
# if num has been spoken before
# calculate difference between last spoken turn and current turn
diff = pdict[last][-1] - pdict[last][0]
# if diff value not in dict add it
if diff not in pdict:
pdict[diff] = [turn]
# else update list for spoken num
pdict[diff] = [pdict[diff][-1], turn]
last = diff
return last
print(f'Part 1: {solve(2020)}')
print(f'Part 2: {solve(30000000)}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment