-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday7p1.py
45 lines (39 loc) · 1.07 KB
/
day7p1.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
def eval(nums, ops):
for op in ops:
if op: #plus
nums = [nums[0] + nums[1]] + nums[2:]
else:
nums = [nums[0] * nums[1]] + nums[2:]
return nums[0]
def test_set(target, nums):
ops = [False for i in range(len(nums) - 1)]
possibilities = 2 ** len(ops)
for i in range(possibilities):
if eval(nums, ops) == target:
return True
ops = increment_binary(ops)
return False
def increment_binary(bin):
cout = False
result = []
first = True
for b in bin[::-1]:
if first:
result.append(not b)
cout = b
first = False
else:
result.append((b and not cout) or (cout and not b))
cout = b and cout
return result[::-1]
with open("day7_input.txt", "r") as f:
data = f.read()
data = data.split("\n")
total = 0
for row in data:
partition = row.split(": ")
target = int(partition[0])
nums = [int(num) for num in partition[1].split(" ")]
if test_set(target, nums):
total += target
print(total)