-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday09.py
37 lines (25 loc) · 888 Bytes
/
day09.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from pathlib import Path
import numpy as np
import itertools
lines = ''
with open('input/' + Path(__file__).stem, "r") as text_file:
lines = text_file.read().splitlines()
values = np.array(lines, dtype=np.int64)
PREAMBLE_LENGTH = 25
def part1():
for i in range(PREAMBLE_LENGTH, len(values)):
preamble = values[i - PREAMBLE_LENGTH:i]
combs = [sum(tpl) for tpl in list(itertools.combinations(preamble, 2))]
if(values[i] not in combs):
# invalid
return i, values[i]
def part2():
index, val = part1()
for length in range(2, index + 1):
for start in range(0, index - length):
sequence = values[start:start+length]
s = np.sum(sequence)
if(s == val):
return np.sum([np.min(sequence), np.max(sequence)])
print(f'Part 1: {part1()}')
print(f'Part 2: {part2()}')