mirror of
https://github.com/peter-tanner/advent-of-code-2020.git
synced 2024-11-30 10:50:17 +08:00
day 15 with two solutions: slow and LUT approaches
This commit is contained in:
parent
af6a172e81
commit
b7255aa1aa
38
15/a.py
Normal file
38
15/a.py
Normal file
|
@ -0,0 +1,38 @@
|
|||
arr = [ int(x) for x in open('input').read().split(',') ]
|
||||
idx = 30000000
|
||||
|
||||
def lookback(n, arr):
|
||||
arr.reverse()
|
||||
A = 0; c = 0
|
||||
for x in arr:
|
||||
c += 1
|
||||
if x == n:
|
||||
if A == 0:
|
||||
A = c
|
||||
else:
|
||||
return c - A
|
||||
return 0
|
||||
|
||||
def a(arr, idx):
|
||||
for t in range(len(arr),idx):
|
||||
arr.append(lookback(arr[t-1], arr[:t]))
|
||||
return arr[idx-1]
|
||||
|
||||
# print(a(arr, idx))
|
||||
# OK the lookback approach is slow AF. Let's try arr different method!
|
||||
|
||||
def lut_search(lut, n, idx):
|
||||
if n in lut:
|
||||
d = idx - lut[n]
|
||||
lut[n] = idx
|
||||
return d
|
||||
else:
|
||||
lut.update({ n: idx })
|
||||
return 0
|
||||
def b(arr, idx):
|
||||
lut = { arr[x]:x+1 for x in range(len(arr)) }
|
||||
for t in range(len(arr),idx):
|
||||
arr.append(lut_search(lut, arr[t-1], t))
|
||||
return arr[idx-1]
|
||||
|
||||
print(b(arr, idx))
|
Loading…
Reference in New Issue
Block a user