day 9: optimizations and beautifications
This commit is contained in:
parent
31eeead0f7
commit
0bfa735129
13
day09.py
13
day09.py
@ -8,14 +8,12 @@ TEST_SOLUTION_PART2 = 62
|
|||||||
|
|
||||||
def part1(test_mode=False):
|
def part1(test_mode=False):
|
||||||
my_input = aoclib.getInputAsArray(day=DAY, return_type=int, test=test_mode)
|
my_input = aoclib.getInputAsArray(day=DAY, return_type=int, test=test_mode)
|
||||||
if test_mode:
|
max_buffer_size = 5 if test_mode else 25
|
||||||
preamble = 5
|
|
||||||
else:
|
|
||||||
preamble = 25
|
|
||||||
|
|
||||||
number_buffer = []
|
number_buffer = []
|
||||||
for number in my_input:
|
for number in my_input:
|
||||||
if len(number_buffer) == preamble:
|
current_buffer_size = len(number_buffer)
|
||||||
|
if current_buffer_size == max_buffer_size:
|
||||||
found_sum = False
|
found_sum = False
|
||||||
for combination in itertools.combinations(number_buffer, 2):
|
for combination in itertools.combinations(number_buffer, 2):
|
||||||
if sum(combination) == number:
|
if sum(combination) == number:
|
||||||
@ -26,7 +24,7 @@ def part1(test_mode=False):
|
|||||||
return number
|
return number
|
||||||
|
|
||||||
number_buffer.append(number)
|
number_buffer.append(number)
|
||||||
if len(number_buffer) > preamble:
|
if current_buffer_size + 1 > max_buffer_size:
|
||||||
number_buffer = number_buffer[1:]
|
number_buffer = number_buffer[1:]
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
@ -38,6 +36,9 @@ def part2(test_mode=False):
|
|||||||
|
|
||||||
for start_index in range(0, len(my_input)):
|
for start_index in range(0, len(my_input)):
|
||||||
for stop_index in range(start_index + 1, len(my_input) - start_index - 1):
|
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:
|
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 min(my_input[start_index:stop_index]) + max(my_input[start_index:stop_index])
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user