2020-12-15 - Rambunctious Recitation
(original .ipynb)
Day 15 puzzle input is a comma-separated sequence of integers (mine is here) which represent the first few numbers spoken as part of an elf memory game. Part 1 involves playing the game until turn 2020
- and the last number spoken is the answer. Part 2 involves running the game for 30 million turns, and the last number spoken is the answer.
test_input_str = """0,3,6""" puzzle_input_str = open("puzzle_input/day15.txt", "r").read() def solve(input_str, n=2020): input_lst = [int(s) for s in input_str.split(",")] last_seen = {s:i for i, s in enumerate(input_lst[:-1])} turn = len(input_lst) number_spoken = input_lst[-1] #----------------------- while turn < n: if not (number_spoken in last_seen): new_number_spoken = 0 else: new_number_spoken = turn - 1 - last_seen[number_spoken] last_seen[number_spoken] = turn - 1 number_spoken = new_number_spoken turn += 1 #----------------------- return number_spoken assert 436 == solve(test_input_str) # Given the starting numbers 1,3,2, the 2020th number spoken is 1. # Given the starting numbers 2,1,3, the 2020th number spoken is 10. # Given the starting numbers 1,2,3, the 2020th number spoken is 27. # Given the starting numbers 2,3,1, the 2020th number spoken is 78. # Given the starting numbers 3,2,1, the 2020th number spoken is 438. # Given the starting numbers 3,1,2, the 2020th number spoken is 1836. assert 1 == solve("1,3,2") assert 10 == solve("2,1,3") assert 27 == solve("1,2,3") assert 78 == solve("2,3,1") assert 438 == solve("3,2,1") assert 1836 == solve("3,1,2") solve(puzzle_input_str)
232
%%time # Given 0,3,6, the 30000000th number spoken is 175594. # Given 1,3,2, the 30000000th number spoken is 2578. # Given 2,1,3, the 30000000th number spoken is 3544142. # Given 1,2,3, the 30000000th number spoken is 261214. # Given 2,3,1, the 30000000th number spoken is 6895259. # Given 3,2,1, the 30000000th number spoken is 18. # Given 3,1,2, the 30000000th number spoken is 362. assert 175594 == solve("0,3,6", 30_000_000) assert 2578 == solve("1,3,2", 30_000_000) assert 3544142 == solve("2,1,3", 30_000_000) assert 261214 == solve("1,2,3", 30_000_000) assert 6895259 == solve("2,3,1", 30_000_000) assert 18 == solve("3,2,1", 30_000_000) assert 362 == solve("3,1,2", 30_000_000) solve(puzzle_input_str, 30_000_000)
CPU times: user 2min 37s, sys: 2.7 s, total: 2min 40s Wall time: 2min 42s
18929178