-
Notifications
You must be signed in to change notification settings - Fork 0
/
10.py
64 lines (44 loc) · 1.18 KB
/
10.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
from collections import defaultdict
from pathlib import Path
def read():
path = Path(__file__).parent / "input10.txt"
adapters = [0]
with open(path) as f:
for line in f.readlines():
line = int(line.strip())
adapters.append(line)
adapters.sort()
return adapters
def p1(adapters):
jolt1_count = 0
# include ur device built-in adapter
jolt3_count = 1
for x in range(1, len(adapters)):
if adapters[x] - adapters[x - 1] == 3:
jolt3_count += 1
else:
jolt1_count += 1
return jolt1_count * jolt3_count
def p2(adapters):
adapters = set(adapters)
memo = defaultdict(lambda: -1)
largest = max(adapters)
def helper(current):
if current == largest:
return 1
if memo[current] != -1:
return memo[current]
total = 0
for x in range(current + 1, current + 4):
if x in adapters:
total += helper(x)
memo[current] = total
return total
helper(0)
return memo[0]
def main():
input = read()
print(p1(input))
print(p2(input))
if __name__ == "__main__":
main()