Skip to content

Commit b77e29e

Browse files
committed
day 19
1 parent c38a2b3 commit b77e29e

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
|[16](https://adventofcode.com/2020/day/16)|Ticket Translation|[py](/day16/main.py), [alt](/day16/alt.py)|
2121
|[17](https://adventofcode.com/2020/day/17)|Conway Cubes|[py](/day17/main.py)|
2222
|[18](https://adventofcode.com/2020/day/18)|Operation Order|[py](/day18/main.py)|
23-
|[19](https://adventofcode.com/2020/day/19)|-|-|
23+
|[19](https://adventofcode.com/2020/day/19)|Monster Messages|[py](/day19/main.py)|
2424
|[20](https://adventofcode.com/2020/day/20)|-|-|
2525
|[21](https://adventofcode.com/2020/day/21)|-|-|
2626
|[22](https://adventofcode.com/2020/day/22)|-|-|

day19/main.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import re
2+
3+
def build_regex(d, rulenum, is_part1):
4+
if not is_part1:
5+
if rulenum == "8":
6+
return build_regex(d, "42", is_part1) + "+"
7+
elif rulenum == "11":
8+
a = build_regex(d, "42", is_part1)
9+
b = build_regex(d, "31", is_part1)
10+
return "(?:" + "|".join(f"{a}{{{n}}}{b}{{{n}}}" for n in range(1, 100)) + ")"
11+
12+
rule = d[rulenum]
13+
if re.fullmatch(r'"."', rule):
14+
return rule[1]
15+
else:
16+
parts = rule.split(" | ")
17+
res = []
18+
for part in parts:
19+
nums = part.split(" ")
20+
res.append("".join(build_regex(d, n, is_part1) for n in nums))
21+
return "(?:" + "|".join(res) + ")"
22+
23+
with open("input.txt") as f:
24+
rules, msgs = [l.rstrip("\n") for l in f.read().split("\n\n")]
25+
26+
d = dict([rule.split(": ") for rule in rules.split("\n")])
27+
msgs = [x.strip() for x in msgs.split("\n")]
28+
29+
for is_part1 in [True, False]:
30+
z = build_regex(d, "0", is_part1)
31+
print(sum([bool(re.fullmatch(z, msg)) for msg in msgs]))

0 commit comments

Comments
 (0)