Solution to problem 15 part 2 in Python

This commit is contained in:
David Doblas Jiménez 2021-12-23 12:22:16 +01:00
parent d6233b113a
commit 467736d89e

View File

@ -71,18 +71,37 @@ for idx, number in enumerate(sequence):
numbers[number].append(idx)
def part_1() -> None:
last = list(numbers.keys())[-1]
for i in range(len(sequence), 2020):
def part_1_and_2(num: int) -> None:
last = 0 # list(numbers.keys())[-1].copy()
for i in range(len(sequence), num):
if len(numbers[last]) < 2:
numbers[0].append(i)
last = 0
else:
last = numbers[last][-1] - numbers[last][-2]
numbers[last].append(i)
# because of indexing starts at 0
if i == 2019:
print(f"The {i+1}th number spoken is {last}")
print(f"The 2020th number spoken is {last}")
print(f"The {num}th number spoken is {last}")
# --- Part Two ---
# Impressed, the Elves issue you a challenge: determine the 30000000th number
# spoken. For example, given the same starting numbers as above:
# 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.
# Given your starting numbers, what will be the 30000000th number spoken?
if __name__ == "__main__":
part_1()
part_1_and_2(30_000_000)