We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent bd190b1 commit 0e3d780Copy full SHA for 0e3d780
day7.py
@@ -0,0 +1,25 @@
1
+from itertools import product
2
+
3
+with open("inputs/day7.txt", "r") as f:
4
+ inputLines = f.readlines()
5
6
+totalSum = 0
7
8
+for line in inputLines:
9
+ correctSum = int(line.split(": ")[0])
10
+ numbers = [int(num) for num in line.split(": ")[1].strip().split()]
11
+ allPossibleCombinations = product("+*", repeat=len(numbers) - 1)
12
13
+ for addMul in allPossibleCombinations:
14
+ currentValue = numbers[0]
15
+ for i, addOrMul in enumerate(addMul):
16
+ if addOrMul == "+":
17
+ currentValue += numbers[i + 1]
18
+ elif addOrMul == "*":
19
+ currentValue *= numbers[i + 1]
20
21
+ if currentValue == correctSum:
22
+ totalSum += correctSum
23
+ break
24
25
+print(totalSum)
0 commit comments