Day 15 (refactoring)

This commit is contained in:
Sebastian Seedorf
2020-12-15 21:18:55 +01:00
parent 11d1b8c4c6
commit 5a2b9fe23f
3 changed files with 25 additions and 28 deletions

21
day15/common.py Normal file
View File

@@ -0,0 +1,21 @@
from functools import reduce
def yield_numbers(line: str, target: int):
nums = {}
turn = 0
last_spoken = 0
for starting in map(int, line.split(",")):
nums[last_spoken] = turn
turn += 1
last_spoken = starting
yield turn, last_spoken
for t in range(turn, target):
n = t - nums[last_spoken] if last_spoken in nums else 0
nums[last_spoken] = t
last_spoken = n
yield t+1, last_spoken
def get_target(line: str, target: int):
return reduce(lambda a, b: b, yield_numbers(line, target))