-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpartTwo.py
More file actions
26 lines (19 loc) · 801 Bytes
/
Copy pathpartTwo.py
File metadata and controls
26 lines (19 loc) · 801 Bytes
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
from common import *
import re
def partTwo(instr: str) -> int:
# patch input
instr = instr.replace(
"8: 42", "8: 42 | 42 8"
) # the new rule 8 can be expressed as 42 | 42+
instr = instr.replace("11: 42 31", "11: 42 31 | 42 11 31")
rules, messages = parse(instr)
# Since we have sections of our ruleset, we have markers in the regex returned by `make_ruleset_regex`
# that denote where we need to insert a copy of the rule 11 regular expression (which also happens to
# have one of those markers in it)
rr = make_ruleset_regex(rules)
eleven_regex = generate_rule_regex(rules, 11)
for _ in range(10):
rr = rr.replace(replace_marker, eleven_regex)
# run as usual
rule_regex = re.compile(rr)
return run(messages, rule_regex)