optimized versions of day 15

interesting discoveries:
  * using a list instead of a dict made a difference, but not a lot
  * declaring the code in a function instead of as "global code" made *A
    LOT* of difference. I guess it has to do with the variables being
    global in that case
This commit is contained in:
Juan José Gutiérrez de Quevedo Pérez 2020-12-15 11:40:03 +01:00
parent 041b87cd05
commit dd2497438d
2 changed files with 40 additions and 34 deletions

21
15/1.py
View file

@ -1,21 +1,24 @@
content = [2,0,6,12,1,3]
def play_number_of_turns(turns):
content = [2,0,6,12,1,3]
spoken = {}
new_spoken = 0
turn = 1
for c in content:
spoken = [0] * turns
new_spoken = 0
turn = 1
for c in content:
spoken[c] = turn
turn += 1
last_turn = 0
while turn <= 2020:
last_turn = 0
while turn <= turns:
if not last_turn:
new_spoken = 0
else:
new_spoken = turn - 1 - last_turn
last_turn = spoken.get(new_spoken, 0)
last_turn = spoken[new_spoken]
spoken[new_spoken] = turn
turn += 1
print(new_spoken)
print(new_spoken)
play_number_of_turns(2020)

21
15/2.py
View file

@ -1,21 +1,24 @@
content = [2,0,6,12,1,3]
def play_number_of_turns(turns):
content = [2,0,6,12,1,3]
spoken = {}
new_spoken = 0
turn = 1
for c in content:
spoken = [0] * turns
new_spoken = 0
turn = 1
for c in content:
spoken[c] = turn
turn += 1
last_turn = 0
while turn <= 30000000:
last_turn = 0
while turn <= turns:
if not last_turn:
new_spoken = 0
else:
new_spoken = turn - 1 - last_turn
last_turn = spoken.get(new_spoken, 0)
last_turn = spoken[new_spoken]
spoken[new_spoken] = turn
turn += 1
print(new_spoken)
print(new_spoken)
play_number_of_turns(30000000)