Day 19 (cleanup and reduce depth for speedup)
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
lines = (x.strip() for x in open("input.txt"))
|
||||
rules = {}
|
||||
sum = 0
|
||||
|
||||
|
||||
def yield_rule(line, rule):
|
||||
@@ -23,7 +24,6 @@ def check(line, idx):
|
||||
yield from yield_rule(line, rule)
|
||||
|
||||
|
||||
sum = 0
|
||||
for line in lines:
|
||||
if ":" in line:
|
||||
idx, r = line.split(": ")
|
||||
@@ -35,4 +35,3 @@ for line in lines:
|
||||
if rest == "":
|
||||
sum += 1
|
||||
print(sum)
|
||||
exit(0)
|
||||
|
||||
@@ -2,19 +2,20 @@
|
||||
|
||||
lines = (x.strip() for x in open("input.txt"))
|
||||
rules = {}
|
||||
sum = 0
|
||||
|
||||
|
||||
def yield_rule(line, rule, depth):
|
||||
if len(rule) > 0:
|
||||
elem, *tail = rule
|
||||
for rest in check(line, elem, depth+1):
|
||||
yield from yield_rule(rest, tail, depth)
|
||||
for rest in check(line, elem, depth-1):
|
||||
yield from yield_rule(rest, tail, depth-1)
|
||||
else:
|
||||
yield line
|
||||
|
||||
|
||||
def check(line, idx, depth=0):
|
||||
if depth > 100:
|
||||
def check(line, idx, depth):
|
||||
if depth < 0:
|
||||
return
|
||||
rs = rules[idx]
|
||||
if isinstance(rs, str):
|
||||
@@ -25,7 +26,6 @@ def check(line, idx, depth=0):
|
||||
yield from yield_rule(line, rule, depth)
|
||||
|
||||
|
||||
sum = 0
|
||||
for line in lines:
|
||||
line = '11: 42 31 | 42 11 31' if line.startswith('11:') else line
|
||||
line = '8: 42 | 42 8' if line.startswith('8:') else line
|
||||
@@ -35,8 +35,7 @@ for line in lines:
|
||||
r = r[1:len(r)-1] if r.startswith('"') else [[int(num) for num in x.split()] for x in r.split(" | ")]
|
||||
rules[idx] = r
|
||||
elif line != "":
|
||||
for rest in check(line, 0):
|
||||
for rest in check(line, 0, len(line)):
|
||||
if rest == "":
|
||||
sum += 1
|
||||
print(sum)
|
||||
exit(0)
|
||||
|
||||
Reference in New Issue
Block a user