master
E. Almqvist 4 years ago
parent 6a2590f66e
commit 4890aa4c3c
  1. 13
      2020/9.py

@ -3,14 +3,14 @@
from aoc import get_input # AoC from aoc import get_input # AoC
import re # regex import re # regex
nums = list(map(int, get_input(9).splitlines() )) nums = list(map(int, get_input(9).splitlines()))
def checkValid(nums, numsum): def checkValid(nums, numsum):
for num1 in nums: for num1 in nums:
for num2 in nums: for num2 in nums:
if( num1 + num2 == numsum and num1 != num2 ): if( num1 + num2 == numsum and num1 != num2 ):
return [num1, num2], numsum return [num1, num2], numsum
return False, numsum return False, numsum
@ -32,20 +32,13 @@ for i in range(len(nums)):
invalid, invalidindex = valid[1], i invalid, invalidindex = valid[1], i
break break
def sumlist(l):
ssum = 0
for num in l:
ssum += num
return ssum
stop = False stop = False
foundnums = [] foundnums = []
for i in range(len(nums)): for i in range(len(nums)):
for rangei in range(len(nums)): for rangei in range(len(nums)):
numlist = nums[i-rangei:i] numlist = nums[i-rangei:i]
if( not invalid in numlist ): if( not invalid in numlist ):
sumnums = sumlist(numlist) sumnums = sum(numlist)
if(sumnums == invalid): if(sumnums == invalid):
foundnums = numlist foundnums = numlist

Loading…
Cancel
Save