-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsol10.py
80 lines (57 loc) · 2.33 KB
/
sol10.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
68
69
70
71
72
73
74
75
76
77
78
79
80
import utils
from collections import Counter
def initialise():
global adapters, device
adapters = utils.loadInputFile("input_10.txt")
adapters = [int(adapters) for adapters in adapters]
# a adapter can take 1,2,3 jolts lower input
# device jolts = 3 jolts higher than the highest rated adapter
# plane charging outlet is 0 jolts
device = max(adapters) + 3
adapters.sort()
def contructPossiblePathways(adapters):
global adapterPathways
# insert the planes charging outlet at start of list
adapters.insert(0, 0)
# initialise dictionary of pathways
adapterPathways = {}
for adapter in adapters:
adapterPathways[adapter] = []
# iterate through sorted adapters
for idx, adapter in enumerate(adapters):
# iterate though indices from the next until the end of the adapter list
# for subsequentAdapter in range(idx + 1, len(adapters)):
for subsequentAdapter in adapters[idx+1:]:
# if subsequentAdapter exceeds a difference of 3 jolts, proceed to next
if abs(adapter - subsequentAdapter) > 3:
break
# add the current adapter
adapterPathways[subsequentAdapter].append(adapter)
def part1():
global adapters, device
previousAdapter = 0
joltDifferences = []
for adapter in adapters:
joltDifferences.append(abs(previousAdapter - adapter))
previousAdapter = adapter
joltDifferences.append(abs(previousAdapter - device))
distribution = Counter(joltDifferences)
return distribution[1] * distribution[3]
def part2(adapters):
global adapterPathways
adapterCombinations = {}
# initialise the plane charging outlet with 1 combination
adapterCombinations[0] = 1
for adapter in adapters[1:]:
combinations = 0
# for each parent adapter, increment by the parents number of combinations
for parentAdapter in adapterPathways[adapter]:
combinations += adapterCombinations[parentAdapter]
# save the number of combinations in dictionary
adapterCombinations[adapter] = combinations
# return the number of combinations for the final adapter
return adapterCombinations[adapters[-1]]
initialise()
contructPossiblePathways(adapters)
print("Part 1:", part1()) # 2046
print("Part 2:", part2(adapters)) # 1157018619904