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):
|
||||
my_input = aoclib.getInputAsArray(day=DAY, return_type=int, test=test_mode)
|
||||
if test_mode:
|
||||
preamble = 5
|
||||
else:
|
||||
preamble = 25
|
||||
max_buffer_size = 5 if test_mode else 25
|
||||
|
||||
number_buffer = []
|
||||
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
|
||||
for combination in itertools.combinations(number_buffer, 2):
|
||||
if sum(combination) == number:
|
||||
@ -26,7 +24,7 @@ def part1(test_mode=False):
|
||||
return number
|
||||
|
||||
number_buffer.append(number)
|
||||
if len(number_buffer) > preamble:
|
||||
if current_buffer_size + 1 > max_buffer_size:
|
||||
number_buffer = number_buffer[1:]
|
||||
|
||||
return 0
|
||||
@ -38,6 +36,9 @@ def part2(test_mode=False):
|
||||
|
||||
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])
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user