This commit is contained in:
Jonas Neugebauer 2020-12-17 20:48:58 +01:00
parent 78736e1fc4
commit 0f6ce4831c
3 changed files with 1059 additions and 0 deletions

37
2020/day9_1.py Normal file
View File

@ -0,0 +1,37 @@
from pprint import pprint
input = []
with open('day9_input.txt', 'r') as f:
for line in f.readlines():
input.append(int(line.strip()))
# prepare prefix cache
prefix = 25
cache = []
for i in range(prefix-1):
cache.append([])
for j in range(prefix-i-1):
cache[i].append(input[i]+input[i+j+1])
def check_val(cache, val):
for c in cache:
if val in c:
return True
return False
# start checking
err = None
for i in range(len(input)-prefix):
val = input[i+prefix]
if check_val(cache, val):
cache = cache[1:]
for j in range(prefix-2):
cache[j].append(input[i+j+1]+val)
cache.append([input[i+prefix-1]+val])
else:
err = i+prefix
break
print(err)
print(input[err-25:err])
print(input[err])

22
2020/day9_2.py Normal file
View File

@ -0,0 +1,22 @@
from pprint import pprint
input = []
with open('day9_input.txt', 'r') as f:
for line in f.readlines():
input.append(int(line.strip()))
# Solution of problem 1
invalid_idx = 511
invalid_val = input[invalid_idx]
def adder(input, low=0, high=2):
s = sum(input[low:high])
if s == invalid_val:
return min(input[low:high])+max(input[low:high])
elif s > invalid_val:
return adder(input, low+1, high)
else:
return adder(input, low, high+1)
print(adder(input))

1000
2020/day9_input.txt Normal file

File diff suppressed because it is too large Load Diff