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

View file

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

View file

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