add remaining code and input files from 2020
This commit is contained in:
1 parent
c10382b0c0
commit
23b2e01280
41 files changed
+11320
-37
No files matched your search
@@ -0,0 +1,39 @@
|
||||
|
||||
|
||||
# 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)))
|
||||
Reference in new issue
Block a user