-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday9.py
67 lines (44 loc) · 1.21 KB
/
day9.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
PREAMBLE = 25
with open("./data/day9.txt") as f:
data = f.read()
data = data.split("\n")
data = filter(lambda s: len(s) > 0, data)
data = map(int, data)
def check_index(data, i):
items = data[max(0, i - PREAMBLE) : i]
item = data[i]
return is_multiple(items, item)
def is_multiple(data, value):
for i, x in enumerate(data):
for j, y in enumerate(data):
if i == j:
continue
if x + y == value:
return True
return False
data = list(data)
def part1():
for i in range(PREAMBLE, len(data)):
value = data[i]
valid = check_index(data, i)
if not valid:
print("not valid", value)
return value
def is_valid_sum(data, value):
for i, x in enumerate(data):
subset = data[:i]
result = sum(subset)
if result > value:
break
if result == value:
return subset
return False
def part2(value):
for i in range(len(data)):
subset = data[i:]
valid = is_valid_sum(subset, value)
if valid:
print(valid)
print('result is', min(valid) + max(valid))
value = part1()
part2(value)