-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart_b.py
executable file
·84 lines (57 loc) · 1.84 KB
/
part_b.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python3
from abc import ABC
import utils
from year_2020.day_18 import part_a
class Challenge(utils.BaseChallenge):
part_a_for_testing = part_a
def solve(self, _input, debug=False):
"""
>>> Challenge().default_solve()
283582817678281
"""
return int(ExpressionSet2.from_expressions_text(_input))
class ExpressionSet2(part_a.ExpressionSet):
pass
class Expression2(part_a.Expression, ABC):
"""
>>> int(Expression2.from_expression_text("1 + 2 * 3 + 4 * 5 + 6"))
231
>>> int(Expression2.from_expression_text(
... "1 + (2 * 3) + (4 * (5 + 6))"))
51
>>> int(Expression2.from_expression_text(
... "2 * 3 + (4 * 5)"))
46
>>> int(Expression2.from_expression_text(
... "5 + (8 * 3 + 9 + 3 * 4 * 3)"))
1445
>>> int(Expression2.from_expression_text(
... "5 * 9 * (7 * 3 * 3 + 9 * 3 + (8 + 6 * 4))"))
669060
>>> int(Expression2.from_expression_text(
... "((2 + 4 * 9) * (6 + 9 * 8 + 6) + 6) + 2 + 4 * 2"))
23340
"""
@classmethod
def intercept_text(cls, expression_text):
intercepted = cls.re_number.sub(cls.replace_number, expression_text)
intercepted = intercepted.replace('+', '?')
intercepted = intercepted.replace('*', '+')
intercepted = intercepted.replace('?', '*')
return intercepted
def __add__(self, other):
return self.mul_class((self, other))
def __mul__(self, other):
return self.add_class((self, other))
ExpressionSet2.expression_class = Expression2
class Number2(Expression2, part_a.Number):
pass
Expression2.number_class = Number2
class Add2(Expression2, part_a.Add):
pass
Expression2.add_class = Add2
class Mul2(Expression2, part_a.Mul):
pass
Expression2.mul_class = Mul2
Challenge.main()
challenge = Challenge()