Solve day 9

This commit is contained in:
Gabriel Augendre 2020-12-09 08:27:55 +01:00
parent 755b6628ea
commit 039182bea9
No known key found for this signature in database
GPG key ID: 1E693F4CE4AEE7B4
3 changed files with 1078 additions and 0 deletions

57
2020/day09-xmas.py Normal file
View file

@ -0,0 +1,57 @@
import copy
import itertools
from typing import List
def main(
filename: str,
expected_part_1: int = None,
expected_part_2: int = None,
preamble: int = 25,
):
print(f"\n+ Running on {filename}")
with open(filename) as f:
numbers = list(map(int, f.read().strip().split("\n"))) # type: List[int]
pointer = preamble
while pointer < len(numbers) and matches_rule(numbers, pointer, preamble):
pointer += 1
counter_part_1 = numbers[pointer]
range_part_2 = sorted(find_range(numbers, counter_part_1))
counter_part_2 = range_part_2[0] + range_part_2[-1]
print(f"1. Found {counter_part_1}")
if expected_part_1:
assert expected_part_1 == counter_part_1
print(f"2. Found {counter_part_2}")
if expected_part_2:
assert expected_part_2 == counter_part_2
def matches_rule(numbers, pointer, preamble):
range_start = pointer - preamble
range_end = pointer
number = numbers[pointer]
for combination in itertools.combinations(numbers[range_start:range_end], 2):
if number == sum(combination):
return True
return False
def find_range(numbers, found_in_step_1):
for i, start in enumerate(numbers):
total = start
j = i + 1
while total < found_in_step_1 and j < len(numbers):
total += numbers[j]
j += 1
if total == found_in_step_1:
return numbers[i:j]
raise Exception("Didn't find a valid range")
if __name__ == "__main__":
main("inputs/day09-test1", 127, 62, preamble=5)
main("inputs/day09", 144381670)

1001
2020/inputs/day09 Normal file

File diff suppressed because it is too large Load diff

20
2020/inputs/day09-test1 Normal file
View 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