46 lines
1.3 KiB
Python
46 lines
1.3 KiB
Python
import aoclib
|
|
import itertools
|
|
|
|
DAY = 9
|
|
TEST_SOLUTION_PART1 = 127
|
|
TEST_SOLUTION_PART2 = 62
|
|
|
|
|
|
def part1(test_mode=False):
|
|
my_input = aoclib.getInputAsArray(day=DAY, return_type=int, test=test_mode)
|
|
max_buffer_size = 5 if test_mode else 25
|
|
|
|
number_buffer = []
|
|
for number in my_input:
|
|
current_buffer_size = len(number_buffer)
|
|
if current_buffer_size == max_buffer_size:
|
|
found_sum = False
|
|
for combination in itertools.combinations(number_buffer, 2):
|
|
if sum(combination) == number:
|
|
found_sum = True
|
|
break
|
|
|
|
if not found_sum:
|
|
return number
|
|
|
|
number_buffer.append(number)
|
|
if current_buffer_size + 1 > max_buffer_size:
|
|
number_buffer = number_buffer[1:]
|
|
|
|
return 0
|
|
|
|
|
|
def part2(test_mode=False):
|
|
my_input = aoclib.getInputAsArray(day=DAY, return_type=int, test=test_mode)
|
|
sum_to_find = part1(test_mode)
|
|
|
|
for start_index in range(0, len(my_input)):
|
|
for stop_index in range(start_index + 1, len(my_input) - start_index - 1):
|
|
if my_input[stop_index] == sum_to_find:
|
|
break
|
|
|
|
if sum(my_input[start_index:stop_index]) == sum_to_find:
|
|
return min(my_input[start_index:stop_index]) + max(my_input[start_index:stop_index])
|
|
|
|
return 0
|