From b7255aa1aae26945767e1324ff096c8d875df089 Mon Sep 17 00:00:00 2001 From: npc-strider Date: Tue, 15 Dec 2020 16:37:03 +0800 Subject: [PATCH] day 15 with two solutions: slow and LUT approaches --- 15/a.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 15/a.py diff --git a/15/a.py b/15/a.py new file mode 100644 index 0000000..e16f10f --- /dev/null +++ b/15/a.py @@ -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)) \ No newline at end of file