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

51 lines
1.2 KiB

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