This repository has been archived by the owner on May 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
18_operation_order.py
69 lines (51 loc) · 1.89 KB
/
18_operation_order.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
###################################
# --- Day 18: Operation Order --- #
###################################
import AOCUtils
# Special int subclass for Part 1: for + and * to have the same precedence,
# replace all '*' with '-' but change __sub__ behavior to __mul__.
class int1(int):
repl = {"*": "-"}
def __add__(self, other): return int1(super().__add__(other))
def __sub__(self, other): return int1(super().__mul__(other))
# Special int subclass for Part 2: for + to have a higher precedence than *,
# swap both '*' and '+' but swap their behaviors as well.
class int2(int):
repl = {"*": "+", "+": "*"}
def __add__(self, other): return int2(super().__mul__(other))
def __mul__(self, other): return int2(super().__add__(other))
def splitTokens(expr):
splitExpr = []
i = 0
j = 0
while j < len(expr):
if not expr[j].isdigit():
splitExpr.append(expr[j])
j += 1
else:
while j < len(expr) and expr[j].isdigit(): j += 1
splitExpr.append(expr[i:j])
i = j
return splitExpr
def specialEval(expr, cls):
expr = expr.replace(" ", "")
# Replace operations according to cls
replacedExpr = list(expr)
for i in range(len(replacedExpr)):
for old, new in cls.repl.items():
if expr[i] == old: replacedExpr[i] = new
expr = "".join(replacedExpr)
# expr.split(), but keep digits together
expr = splitTokens(expr)
# Replace numbers with instances of cls
for i in range(len(expr)):
if expr[i].isdigit():
expr[i] = "{}({})".format(cls.__name__, expr[i])
return eval("".join(expr))
###################################
homework = AOCUtils.loadInput(18)
p1 = sum(specialEval(expr, int1) for expr in homework)
print("Part 1: {}".format(p1))
p2 = sum(specialEval(expr, int2) for expr in homework)
print("Part 2: {}".format(p2))
AOCUtils.printTimeTaken()