-
Notifications
You must be signed in to change notification settings - Fork 0
/
day19_part2.py
executable file
·65 lines (55 loc) · 1.81 KB
/
day19_part2.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
#!/usr/bin/python3
import loader
import re
from functools import lru_cache
inp = loader.blank_line_delimited("input_day19")
raw_rules = inp[0]
raw_messages = inp[1]
rules = {}
for raw_rule in raw_rules:
m = re.match(r'^(\d+): (.*)',raw_rule)
rule_number = int(m.group(1))
raw_definition = m.group(2)
rules[rule_number] = raw_definition
#part 2 modifications:
rules[8] = "42 | 42 8"
rules[11] = "42 31 | 42 11 31"
@lru_cache(maxsize=None)
def rule_to_regex(rule_number):
if rule_number == 8:
return "(" + rule_to_regex(42) + ")+"
if rule_number == 11:
max_message_length = max([len(message) for message in raw_messages])
regex_text = "("
for i in range(1,max_message_length // 2):
regex_text += "(" + rule_to_regex(42) + "){" f"{i},{i}" + "}(" + rule_to_regex(31) + "){" f"{i},{i}" + "}" + "|"
regex_text = regex_text[0:-1] + ")"
return regex_text
if rules[rule_number] == '"a"':
return "a"
if rules[rule_number] == '"b"':
return "b"
if "|" not in rules[rule_number]:
regex_text = ""
for sub_rule in rules[rule_number].split(" "):
regex_text += rule_to_regex(int(sub_rule))
return regex_text
# compound rule
split_rule = rules[rule_number].split("|")
lhs = split_rule[0].strip()
rhs = split_rule[1].strip()
regex_text = "("
for sub_rule in lhs.split(" "):
regex_text += rule_to_regex(int(sub_rule))
regex_text += "|"
for sub_rule in rhs.split(" "):
regex_text += rule_to_regex(int(sub_rule))
regex_text += ")"
return regex_text
regex_text = "^" + rule_to_regex(0) + "$"
compiled_regex = re.compile(regex_text)
match_count = 0
for message in raw_messages:
if compiled_regex.match(message):
match_count += 1
print(match_count)