part 2 day 15

This commit is contained in:
2020-12-17 08:28:11 +01:00
parent 2279e69e9a
commit ee85057660

View File

@@ -12,7 +12,7 @@ def part1():
for i in range(0, final_round): for i in range(0, final_round):
if i < len(numbers): if i < len(numbers):
print(f'Turn {i + 1}: {numbers[i]}') # print(f'Turn {i + 1}: {numbers[i]}')
last_spoken = numbers[i] last_spoken = numbers[i]
history[last_spoken] = [i] history[last_spoken] = [i]
continue continue
@@ -34,8 +34,38 @@ def part1():
def part2(): def part2():
pass file = open("input.txt")
# print("Part 2: ") lines = [i.strip() for i in file.readlines()]
numbers = lines[0].split(',')
history = {}
final_round = 30000000
last_spoken = ''
for i in range(0, final_round):
if i < len(numbers):
# print(f'Turn {i + 1}: {numbers[i]}')
last_spoken = numbers[i]
history[last_spoken] = [i]
continue
if len(history[last_spoken]) == 1:
last_spoken = '0'
history[last_spoken].append(i)
else:
count = len(history[last_spoken])
last_spoken = str(history[last_spoken][count - 1] - history[last_spoken][count - 2])
if last_spoken not in history:
history[last_spoken] = [i]
else:
history[last_spoken].append(i)
# print(f'Turn {i + 1}: {last_spoken}')
print(f"Part 2: {last_spoken}")
if __name__ == "__main__": if __name__ == "__main__":