2020/Day 9/two.py

60 lines
1.4 KiB
Python
Raw Permalink Normal View History

2022-07-01 15:56:12 +02:00
#!/bin/python
def check(numbers, num):
for i in numbers:
for j in numbers:
if i + j == num:
return True
return False
def rotate(numbers, num):
del numbers[0]
numbers.append(num)
return numbers
def sum_range(all_numbers, i, j):
range = all_numbers[i:j+1]
range.sort()
return range[0] + range[-1]
def find_sum(num, all_numbers):
for i,i_val in enumerate(all_numbers):
j = i + 1
while j < len(all_numbers):
j_val = all_numbers[j]
print(f"Checking {i}:{i_val} to {j}:{j_val}")
if check_sum(all_numbers, i, j+1, num):
print(f"Found {i} to {j} the sum is {sum_range(all_numbers, i, j)}")
return
j += 1
def check_sum(all_numbers, i, j, num):
sum = 0
for to_sum in all_numbers[i:j]:
sum += to_sum
if sum > num:
return False
return sum == num
with open("input", 'r') as input:
all_numbers = [int(i.strip()) for i in input.readlines()]
numbers = []
for i in range(26):
numbers.append(all_numbers[i])
i = 26
while i < len(all_numbers):
num = all_numbers[i]
if not check(numbers, num):
print(num)
find_sum(num, all_numbers)
break
numbers = rotate(numbers, num)
i += 1