-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathd10.py
More file actions
46 lines (36 loc) · 973 Bytes
/
d10.py
File metadata and controls
46 lines (36 loc) · 973 Bytes
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
import fileinput
def get_input():
data = [l.strip() for l in fileinput.input()]
data = list(filter(bool, data))
return data
def preprocess(data):
jolts = sorted(int(v) for v in data)
jolts = [0, *jolts, jolts[-1] + 3]
return jolts
def part1(data):
diffs = []
jolts = preprocess(data)
for i, x in enumerate(jolts):
for y in jolts[i+1:]:
d = y - x
assert d < 4
diffs.append(d)
break
a, b = diffs.count(1), diffs.count(3)
return a * b
def part2(data):
jolts = preprocess(data)
combos = [0] * len(jolts)
combos[0] = combos[1] = 1
for i in range(2, len(jolts)):
x = jolts[i]
for j in range(i-1, -1, -1):
y = jolts[j]
if x - y > 3:
break
combos[i] += combos[j]
return combos[-1]
if __name__ == '__main__':
indata = get_input()
print(part1(indata))
print(part2(indata))