-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAoC_day7.py
49 lines (39 loc) · 1.41 KB
/
AoC_day7.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
import itertools
import operator
from tqdm import tqdm
def parse_file(fn):
targets = []
arrs = []
with open(fn, "r") as fh:
for line in fh:
targets.append(int(line.split(":")[0]))
arrs.append([int(i) for i in line.strip().split(':')[1].split()])
return arrs, targets
def part1(in_arrs, targets):
total = 0
ops = (operator.add, operator.mul)
for in_arr, target in zip(in_arrs, targets):
for op_list in itertools.product(ops, repeat=len(in_arr) - 1):
result = in_arr[0]
for i, op in enumerate(op_list):
result = op(result, in_arr[i + 1])
if result == target:
total += target
break
return total
def part2(in_arrs, targets):
total = 0
ops = (operator.add, operator.mul, lambda a, b: int(str(a) + str(b)))
for in_arr, target in tqdm(zip(in_arrs, targets), total=len(targets)):
for op_list in itertools.product(ops, repeat=len(in_arr) - 1):
result = in_arr[0]
for i, op in enumerate(op_list):
result = op(result, in_arr[i + 1])
if result == target:
total += target
break
return total
in_file = "AoC_input/day7.txt"
arrs, targets = parse_file(in_file)
print("Part.1", part1(arrs, targets))
print("Part.2", part2(arrs, targets))