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