diff --git a/day19/part1.py b/day19/part1.py index 1104fd7..7071483 100644 --- a/day19/part1.py +++ b/day19/part1.py @@ -16,19 +16,18 @@ def yield_rule(line, rule): def check(line, idx): rs = rules[idx] - if isinstance(rs, str): - if line.startswith(rs): - yield line[len(rs):] - else: + if not isinstance(rs, str): for rule in rules[idx]: yield from yield_rule(line, rule) + elif line.startswith(rs): + yield line[len(rs):] for line in lines: if ":" in line: idx, r = line.split(": ") idx = int(idx) - 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 [list(map(int, x.split())) for x in r.split(" | ")] rules[idx] = r elif line != "": for rest in check(line, 0): diff --git a/day19/part2.py b/day19/part2.py index ec1c825..12a9308 100644 --- a/day19/part2.py +++ b/day19/part2.py @@ -18,12 +18,11 @@ def check(line, idx, depth): if depth < 0: return rs = rules[idx] - if isinstance(rs, str): - if line.startswith(rs): - yield line[len(rs):] - else: + if not isinstance(rs, str): for rule in rules[idx]: yield from yield_rule(line, rule, depth) + elif line.startswith(rs): + yield line[len(rs):] for line in lines: @@ -32,7 +31,7 @@ for line in lines: if ":" in line: idx, r = line.split(": ") idx = int(idx) - 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 [list(map(int, x.split())) for x in r.split(" | ")] rules[idx] = r elif line != "": for rest in check(line, 0, len(line)):