day 9
This commit is contained in:
parent
29b0604952
commit
76d238fc7b
55
day09.py
Normal file
55
day09.py
Normal file
@ -0,0 +1,55 @@
|
||||
import aoclib
|
||||
import itertools
|
||||
|
||||
DAY = 9
|
||||
TEST_SOLUTION_PART1 = 127
|
||||
TEST_SOLUTION_PART2 = 62
|
||||
|
||||
|
||||
class NumberCruncher:
|
||||
def __init__(self, preamble):
|
||||
self.buffer = []
|
||||
self.preamble = preamble
|
||||
|
||||
def addNumber(self, number):
|
||||
self.buffer.append(number)
|
||||
if len(self.buffer) > self.preamble:
|
||||
self.buffer = self.buffer[1:]
|
||||
|
||||
def isValidNextNumber(self, number):
|
||||
if len(self.buffer) < self.preamble:
|
||||
return True
|
||||
|
||||
for combination in itertools.combinations(self.buffer, 2):
|
||||
if sum(combination) == number:
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
|
||||
def part1(test_mode=False):
|
||||
my_input = aoclib.getInputAsArray(day=DAY, return_type=int, test=test_mode)
|
||||
if test_mode:
|
||||
cruncher = NumberCruncher(5)
|
||||
else:
|
||||
cruncher = NumberCruncher(25)
|
||||
|
||||
for i in my_input:
|
||||
if not cruncher.isValidNextNumber(i):
|
||||
return i
|
||||
|
||||
cruncher.addNumber(i)
|
||||
|
||||
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 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
|
||||
20
inputs/9_test
Normal file
20
inputs/9_test
Normal file
@ -0,0 +1,20 @@
|
||||
35
|
||||
20
|
||||
15
|
||||
25
|
||||
47
|
||||
40
|
||||
62
|
||||
55
|
||||
65
|
||||
95
|
||||
102
|
||||
117
|
||||
150
|
||||
182
|
||||
127
|
||||
219
|
||||
299
|
||||
277
|
||||
309
|
||||
576
|
||||
Loading…
Reference in New Issue
Block a user