day 9 initial solution
This commit is contained in:
parent
ccc1ac5112
commit
0887dc6904
49
09/09.py
Normal file
49
09/09.py
Normal 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
1000
09/input.txt
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue