-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday10.py
More file actions
73 lines (64 loc) · 2.38 KB
/
day10.py
File metadata and controls
73 lines (64 loc) · 2.38 KB
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
import itertools as it
import math
def Mult1And3JoltJumps(adapters):
joltJumps1 = 0
joltJumps3 = 1
if (min(adapters) == 1): joltJumps1 += 1
elif (min(adapters) == 3): joltJumps3 += 1
for x in range(1, len(adapters)):
if abs(adapters[x] - adapters[x-1]) == 1: joltJumps1 += 1
elif abs(adapters[x] - adapters[x-1]) == 3: joltJumps3 += 1
return joltJumps1 * joltJumps3
def getJoltDifferences(adapters):
joltDifs = [min(adapters)]
for x in range(1, len(adapters)):
joltDifs.append(abs(adapters[x] - adapters[x-1]))
joltDifs.append(3)
return joltDifs
def getRemovableIndices(adapters):
joltsDifs = getJoltDifferences(adapters)
removableIndices = []
for x in range(0, len(adapters)):
if joltsDifs[x] + joltsDifs[x+1] <= 3:
removableIndices.append(x)
return removableIndices
def validateCombination(combo, lowerBound, upperBound):
chain = combo[:]
chain = [lowerBound] + chain + [upperBound]
for x in range(1, len(chain)):
if chain[x] - chain[x-1] > 3:
return False
return True
def findCombinationsForSet(comboSet, lowerBound, upperBound):
numberOfValidCombos = 0
print(comboSet)
if lowerBound > upperBound: lowerBound = 0
for x in range(0, len(comboSet)):
for y in it.combinations(comboSet, x+1):
if validateCombination(list(y), lowerBound, upperBound): numberOfValidCombos += 1
if validateCombination([], lowerBound, upperBound): numberOfValidCombos += 1
return numberOfValidCombos
adapters = [int(line.strip()) for line in open('day10input.txt')]
adapters.sort()
print(Mult1And3JoltJumps(adapters))
removableIndices = getRemovableIndices(adapters)
sets = []
tempSet = [removableIndices[0]]
for x in range(1, len(removableIndices)):
if removableIndices[x] - removableIndices[x-1] == 1:
tempSet.append(removableIndices[x])
else:
sets.append(tempSet[:])
del tempSet[:]
tempSet.append(removableIndices[x])
sets.append(tempSet)
combosPerSet = []
for comboSet in sets:
testSet = []
for combo in comboSet:
testSet.append(adapters[combo])
combosPerSet.append(findCombinationsForSet(testSet, adapters[min(comboSet)-1], adapters[max(comboSet)+1]))
del testSet[:]
print(adapters)
print(combosPerSet)
print(math.prod(combosPerSet))