My solutions for Advent of Code.
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.
adventofcode/2020/9.py

63 lines
1.3 KiB

4 years ago
#!/usr/bin/env python
from aoc import get_input # AoC
import re # regex
nums = list(map(int, get_input(9).splitlines() ))
def checkValid(nums, numsum):
for num1 in nums:
for num2 in nums:
if( num1 + num2 == numsum and num1 != num2 ):
return [num1, num2], numsum
return False, numsum
def checkIfNumValid( amble, index ):
prevNums = nums[index-amble:index]
numsum = nums[index]
sumnumbers, numsum2 = checkValid(prevNums, numsum)
return sumnumbers, numsum2
amble = 25 # NOTE: CHANGE ME TO 25
invalid, invalidindex = None, None
for i in range(len(nums)):
if(i > amble-1):
valid = checkIfNumValid(amble, i)
if( not valid[0] ):
invalid, invalidindex = valid[1], i
break
def sumlist(l):
ssum = 0
for num in l:
ssum += num
return ssum
stop = False
foundnums = []
for i in range(len(nums)):
for rangei in range(len(nums)):
numlist = nums[i-rangei:i]
if( not invalid in numlist ):
sumnums = sumlist(numlist)
if(sumnums == invalid):
foundnums = numlist
stop = True
break
if(stop):
break
minnum = min(foundnums)
maxnum = max(foundnums)
print("Part1:", invalid)
print("Part2:", minnum + maxnum)