-
Notifications
You must be signed in to change notification settings - Fork 0
/
day19_part1.py
executable file
·48 lines (40 loc) · 1.2 KB
/
day19_part1.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
#!/usr/bin/python3
import loader
import re
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
def rule_to_regex(rule_number):
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
part1_regex = "^" + rule_to_regex(0) + "$"
match_count = 0
for message in raw_messages:
if re.match(part1_regex,message):
match_count += 1
print(match_count)