wow that was taking forever

This commit is contained in:
a 2020-12-10 00:39:18 -06:00
parent 30049c7d4b
commit de21254159
3 changed files with 168 additions and 0 deletions

59
10/10.py Normal file
View file

@ -0,0 +1,59 @@
def read_file():
with open("input.txt","r") as f:
return [int(x) for x in f.read().split('\n')]
def part1(adapters):
jolts = [0] + sorted(adapters) # j(port) = 0
d1 = 0
d3 = 1 # j(device) = max + 3
i = 0
while i <= len(adapters):
difference = jolts[i] - jolts[i-1]
if difference == 1:
d1 += 1
elif difference == 2:
d2 += 1
elif difference == 3:
d3 += 1
i += 1
return d1 * d3
"""
def find_all_paths(graph, start, end, path=[]):
path = path + [start]
if start == end:
return [path]
if start not in graph:
return []
paths = []
for node in graph[start]:
if node not in path:
newpaths = find_all_paths(graph, node, end, path)
for newpath in newpaths:
paths.append(newpath)
return paths
"""
def part2(adapters):
jolts = [0] + sorted(adapters)
from collections import Counter
dp = Counter()
dp[0] = 1
jolts = sorted(adapters)
jolts.append(jolts[-1] + 3)
for j in jolts:
dp[j] = dp[j-1] + dp[j-2] + dp[j-3]
return dp[jolts[-1]]
"""
graph = {}
for i, joltage in enumerate(jolts):
graph[joltage] = [x for x in jolts[i+1:i+4] if 0 < x - joltage <= 3] # scan next 3, assuming diff of +1 +1 +1 at most
return len( find_all_paths( graph, 0, max(jolts) ) )
"""
def main():
adapters = read_file()
print(part1(adapters), part2(adapters))
if __name__ == "__main__":
main()

98
10/input.txt Normal file
View file

@ -0,0 +1,98 @@
35
111
135
32
150
5
106
154
41
7
27
117
109
63
64
21
138
98
40
71
144
13
66
48
12
55
119
103
54
78
65
112
39
128
53
140
77
34
28
81
151
125
85
124
2
99
131
59
60
6
94
33
42
93
14
141
92
38
104
9
29
100
52
19
147
49
74
70
84
113
120
91
97
17
45
139
90
116
149
129
87
69
20
24
148
18
58
123
76
118
130
132
75
110
105
1
8
86

11
10/input_sample.txt Normal file
View file

@ -0,0 +1,11 @@
16
10
15
5
1
11
7
19
6
12
4