3
3
import datetime
4
4
5
5
fileName = "10.txt"
6
- max = - 1
7
6
8
- def validateList (adapters ):
7
+ def validateList (adapters , min , max ):
9
8
valid = True
10
9
if adapters == None :
11
10
return False
12
11
if len (adapters ) < 2 :
13
12
return False
14
- if adapters [0 ] != 0 :
13
+ if adapters [0 ] != min :
15
14
return False
16
15
if adapters [- 1 ] != max :
17
16
return False
@@ -53,19 +52,48 @@ def makeId(list):
53
52
i = i + 1
54
53
print (diff1 , diff3 , diff1 * diff3 )
55
54
#part 2
56
- max = numbers [- 1 ]
57
- validOptions = {}
58
-
59
- L = 0
60
- for L in range (len (numbers )):
61
- item = itertools .combinations (numbers , L + 1 )
62
- for combo in item :
63
- comboList = list (combo )
64
- #comboList.sort()
65
- #print(comboList)
66
- if validateList (comboList ):
67
- validOptions [makeId (comboList )] = True
68
- #print("Match")
69
- L = L + 1
70
- print (len (validOptions ))
55
+ unskippable = []
56
+ unskippable .append (0 )
57
+
58
+ for i in range (len (numbers )):
59
+ if i > 0 and (numbers [i ] - numbers [i - 1 ]) == 3 :
60
+ unskippable .append (i )
61
+ else :
62
+ if (i + 1 ) < len (numbers ):
63
+ big = numbers [i + 1 ]
64
+ little = numbers [i ]
65
+ if (big - little ) == 3 :
66
+ unskippable .append (i )
67
+
68
+ problems = []
69
+ for i in range (1 , len (unskippable )):
70
+ pair = [unskippable [i - 1 ], unskippable [i ]]
71
+ problems .append (pair )
72
+ solutions = []
73
+ for i in range (len (problems )):
74
+ solutions .append (0 )
75
+
76
+ #work each problem and save its solution
77
+ for i in range (len (problems )):
78
+ mini = problems [i ][0 ]
79
+ maxi = problems [i ][1 ]
80
+ if maxi - mini == 1 :
81
+ solutions [i ] = 1
82
+ else :
83
+ validOptions = {}
84
+ workSet = numbers [mini :maxi + 1 ]
85
+
86
+ L = 0
87
+ for L in range (len (workSet )):
88
+ item = itertools .combinations (workSet , L + 1 )
89
+ for combo in item :
90
+ comboList = list (combo )
91
+ if validateList (comboList , workSet [0 ], workSet [- 1 ]):
92
+ validOptions [makeId (comboList )] = True
93
+ L = L + 1
94
+ solutions [i ] = len (validOptions )
95
+ total = 1
96
+ for item in solutions :
97
+ total *= item
98
+ print (total )
71
99
print (datetime .datetime .now ())
0 commit comments