From 322454f055169bb319665ef72935fb570dbdd385 Mon Sep 17 00:00:00 2001 From: Sebastian Seedorf Date: Sat, 19 Dec 2020 13:15:57 +0100 Subject: [PATCH] Day 19 (cleanup and reduce depth for speedup) --- day19/part1.py | 3 +-- day19/part2.py | 13 ++++++------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/day19/part1.py b/day19/part1.py index ca0b65f..1104fd7 100644 --- a/day19/part1.py +++ b/day19/part1.py @@ -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) diff --git a/day19/part2.py b/day19/part2.py index 7dc980f..ec1c825 100644 --- a/day19/part2.py +++ b/day19/part2.py @@ -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)