aoc/2020/09/09.py

49 lines
950 B
Python

def read_file():
with open("input.txt","r") as f:
return [int(x) for x in f.read().split('\n')))]
def has_summing_pair(target, numbers):
for number in numbers:
if target - number in numbers:
return True
return False
def part1(numbers):
for i, number in enumerate(numbers):
if i < 25:
continue
past_25_numbers = numbers[i-25:i]
if not has_summing_pair(number, past_25_numbers):
return number
def find_contiguous_sum(target, numbers):
start = 0
end = 0
for i, _ in enumerate(numbers):
start = i
n = i
s = target
while True:
s -= numbers[n]
if s == 0:
end = n
break
if s < 0:
break
n += 1
if end:
break
return min(numbers[start:end]), max(numbers[start:end])
def part2(numbers):
target = part1(numbers)
low, high = find_contiguous_sum(target, numbers)
return low + high
def main():
numbers = read_file()
print(part1(numbers), part2(numbers))
if __name__ == "__main__":
main()