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
37
15/1.py
37
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 = {}
|
||||
new_spoken = 0
|
||||
turn = 1
|
||||
for c in content:
|
||||
spoken[c] = turn
|
||||
turn += 1
|
||||
spoken = [0] * turns
|
||||
new_spoken = 0
|
||||
turn = 1
|
||||
for c in content:
|
||||
spoken[c] = turn
|
||||
turn += 1
|
||||
|
||||
last_turn = 0
|
||||
while turn <= 2020:
|
||||
if not last_turn:
|
||||
new_spoken = 0
|
||||
else:
|
||||
new_spoken = turn - 1 - last_turn
|
||||
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)
|
||||
spoken[new_spoken] = turn
|
||||
turn += 1
|
||||
last_turn = spoken[new_spoken]
|
||||
spoken[new_spoken] = turn
|
||||
turn += 1
|
||||
|
||||
print(new_spoken)
|
||||
print(new_spoken)
|
||||
|
||||
play_number_of_turns(2020)
|
||||
|
|
37
15/2.py
37
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 = {}
|
||||
new_spoken = 0
|
||||
turn = 1
|
||||
for c in content:
|
||||
spoken[c] = turn
|
||||
turn += 1
|
||||
spoken = [0] * turns
|
||||
new_spoken = 0
|
||||
turn = 1
|
||||
for c in content:
|
||||
spoken[c] = turn
|
||||
turn += 1
|
||||
|
||||
last_turn = 0
|
||||
while turn <= 30000000:
|
||||
if not last_turn:
|
||||
new_spoken = 0
|
||||
else:
|
||||
new_spoken = turn - 1 - last_turn
|
||||
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)
|
||||
spoken[new_spoken] = turn
|
||||
turn += 1
|
||||
last_turn = spoken[new_spoken]
|
||||
spoken[new_spoken] = turn
|
||||
turn += 1
|
||||
|
||||
print(new_spoken)
|
||||
print(new_spoken)
|
||||
|
||||
play_number_of_turns(30000000)
|
||||
|
|
Loading…
Reference in a new issue