2020-12-09 06:16:32 +00:00
|
|
|
def read_file():
|
|
|
|
with open("input.txt","r") as f:
|
2020-12-09 08:21:04 +00:00
|
|
|
return [int(x) for x in f.read().split('\n')))]
|
2020-12-09 06:16:32 +00:00
|
|
|
|
|
|
|
def has_summing_pair(target, numbers):
|
2020-12-09 08:16:20 +00:00
|
|
|
for number in numbers:
|
2020-12-09 06:16:32 +00:00
|
|
|
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
|
2020-12-09 08:16:20 +00:00
|
|
|
for i, _ in enumerate(numbers):
|
2020-12-09 06:16:32 +00:00
|
|
|
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()
|