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
# read in aoc input | |
data = open("day15.in").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: | |
break | |
# 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 | |
else: | |
# 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 | |
else: | |
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