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:
parent
041b87cd05
commit
dd2497438d
21
15/1.py
21
15/1.py
|
@ -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 = {}
|
spoken = [0] * turns
|
||||||
new_spoken = 0
|
new_spoken = 0
|
||||||
turn = 1
|
turn = 1
|
||||||
for c in content:
|
for c in content:
|
||||||
spoken[c] = turn
|
spoken[c] = turn
|
||||||
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)
|
||||||
|
|
21
15/2.py
21
15/2.py
|
@ -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 = {}
|
spoken = [0] * turns
|
||||||
new_spoken = 0
|
new_spoken = 0
|
||||||
turn = 1
|
turn = 1
|
||||||
for c in content:
|
for c in content:
|
||||||
spoken[c] = turn
|
spoken[c] = turn
|
||||||
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)
|
||||||
|
|
Loading…
Reference in a new issue