-
Notifications
You must be signed in to change notification settings - Fork 0
/
12_19.py
68 lines (57 loc) · 1.84 KB
/
12_19.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
from time import time
import re
data = [i[:-1] for i in open("num19.txt", "r").readlines()]
def solve(lines):
# Extract input
rules = {}
texts = []
pt = 0
while pt < len(lines):
arr = []
while pt < len(lines) and lines[pt]:
arr.append(lines[pt])
pt += 1
pt += 1
if len(rules) == 0:
for i in arr:
key, val = i.split(": ")
rules[key] = val
else:
texts.extend(arr)
# Apply the new rules
rules["8"] = "42 | 42 8"
rules["11"] = "42 31 | 42 11 31"
def solver(depth_lim):
# Generate the required regex pattern
# Quite a terrifying thought...
# For part 2 I added a depth limit to break out of the infinite loop
# Not a fully-working solution but it works for mine :)))
memo = {}
def helper(id, depth=depth_lim):
if depth == 0:
return ""
if rules[id][0] == '"':
memo[id] = rules[id][1:-1]
return memo[id]
# Concatenate the different patterns
# | is still |
# The others should be wrapped with ()
pat = []
for rule in rules[id].split():
if rule == "|":
pat.append("|")
elif rule == id: # Looping rule
pat.append(helper(rule, depth - 1))
else:
pat.append(helper(rule))
memo[id] = "(%s)" % "".join(pat)
return memo[id]
pattern = helper("0")
return sum(1 for st in texts if re.fullmatch(pattern, st))
return solver(1), solver(5)
t_start = time()
part1, part2 = solve(data)
print("Part 1: %d" % part1)
print("Part 2: %d" % part2)
elapsed = 1000 * (time() - t_start)
print("Time: %.3fms" % elapsed)