day 9 initial solution

This commit is contained in:
a 2020-12-09 00:16:32 -06:00
parent ccc1ac5112
commit 0887dc6904
2 changed files with 1049 additions and 0 deletions

49
09/09.py Normal file
View file

@ -0,0 +1,49 @@
def read_file():
with open("input.txt","r") as f:
return list(map(int, f.read().split('\n')))
def has_summing_pair(target, numbers):
for _, number in enumerate(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, number 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()

1000
09/input.txt Normal file

File diff suppressed because it is too large Load diff