You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

39 lines
991 B

# check if a number is valid (2 in the previous 25 numbers add up to it)
def check_if_valid(prev, num):
for i in prev:
for j in prev:
if i + j == num:
return True
return False
# find a continuous list of numbers that add up to the weakness found earlier
def find_sum(num):
for d in range(0, len(lines)):
for e in range(d, len(lines)):
if sum(lines[d:e]) == num and len(lines[d:e]) > 1:
return lines[d:e]
with open(r"C:\Users\Maya\Desktop\Uni\WS_2021\AoC\input09") as f:
lines = f.readlines()
# convert ti integers
for (a, l) in enumerate(lines):
lines[a] = int(l)
# find weakness
weakness = 0
for k in range(25, len(lines)-1):
if not check_if_valid(lines[(k-25):k], lines[k]):
weakness = lines[k]
break
# challenge 1 print
print(weakness)
# challenge 2 print
print(min(find_sum(weakness)) + max(find_sum(weakness)))