mirror of
https://github.com/peter-tanner/advent-of-code-2020.git
synced 2024-11-30 10:50:17 +08:00
day 9
This commit is contained in:
parent
4cc675c580
commit
bf0cf2f9a6
23
9/a.optimized_attempt.py
Normal file
23
9/a.optimized_attempt.py
Normal file
|
@ -0,0 +1,23 @@
|
|||
import copy
|
||||
import itertools
|
||||
|
||||
preamble_len = 5
|
||||
|
||||
d = [ int(x) for x in open('input').read().splitlines() ]
|
||||
|
||||
index = preamble_len
|
||||
|
||||
def sums(d):
|
||||
return [ sum(x) for x in list(itertools.combinations(d, 2)) ]
|
||||
|
||||
s = sums(d[index-preamble_len:index])
|
||||
for n in d[preamble_len:]:
|
||||
print(d[index-preamble_len:index])
|
||||
print(sorted(sums(d[index-preamble_len:index])))
|
||||
new = [ (x+y) for x in d[index-preamble_len:index-1] if (y := d[index-1]) ]
|
||||
print(new)
|
||||
print(sorted(s))
|
||||
print(n)
|
||||
|
||||
index += 1
|
||||
s = copy.deepcopy(s)[4:]+new #4 is constant, combinations of 2 numbers
|
62
9/a.py
Normal file
62
9/a.py
Normal file
|
@ -0,0 +1,62 @@
|
|||
import copy
|
||||
import itertools
|
||||
|
||||
preamble_len = 25
|
||||
|
||||
d = [ int(x) for x in open('input').read().splitlines() ]
|
||||
|
||||
def sums(d):
|
||||
return [ sum(x) for x in list(itertools.combinations(d, 2)) ]
|
||||
|
||||
def a(d):
|
||||
index = preamble_len
|
||||
for n in d[preamble_len:]:
|
||||
if n not in sums(d[index-preamble_len:index]):
|
||||
print(n)
|
||||
return n
|
||||
break
|
||||
index += 1
|
||||
|
||||
def b(target,d):
|
||||
index = 0
|
||||
current = []
|
||||
for i in range(len(d)):
|
||||
# print('-')
|
||||
# print(current)
|
||||
j = 0
|
||||
pop = []
|
||||
for j_ in current:
|
||||
# print(d[j_:i])
|
||||
s = sum(d[j_:i])
|
||||
if s == target:
|
||||
l = d[j_:i]
|
||||
print([l, min(l) + max(l)])
|
||||
elif s > target:
|
||||
pop.append(j)
|
||||
# print("POP")
|
||||
# print(current)
|
||||
j += 1
|
||||
[ current.pop(0) for x in pop ]
|
||||
current.append(i)
|
||||
|
||||
|
||||
# if acc
|
||||
# print(l)
|
||||
# break
|
||||
# if acc > target:
|
||||
# acc = 0
|
||||
# l = []
|
||||
# l = []
|
||||
# acc = 0
|
||||
# for n in d:
|
||||
# l.append(n)
|
||||
# acc += n
|
||||
# if acc == target and n != target:
|
||||
# print(l)
|
||||
# break
|
||||
# if acc > target:
|
||||
# acc = 0
|
||||
# l = []
|
||||
|
||||
|
||||
b(a(d),d)
|
Loading…
Reference in New Issue
Block a user