diff options
author | Tomasz Kramkowski <tomasz@kramkow.ski> | 2022-12-13 10:36:05 +0000 |
---|---|---|
committer | Tomasz Kramkowski <tomasz@kramkow.ski> | 2022-12-13 10:36:05 +0000 |
commit | ed25f645184821945d1c55e15cff8c93612f330f (patch) | |
tree | 93e84c65e1ea010611348055340827c13c5141db | |
parent | 5f0216b74b70071da7de5d0252a3170039cc2fee (diff) | |
download | aoc2022-ed25f645184821945d1c55e15cff8c93612f330f.tar.gz aoc2022-ed25f645184821945d1c55e15cff8c93612f330f.tar.xz aoc2022-ed25f645184821945d1c55e15cff8c93612f330f.zip |
13 - use pattern matching
-rw-r--r-- | 13.py | 16 |
1 files changed, 7 insertions, 9 deletions
@@ -6,19 +6,17 @@ with open_day(13) as f: pairs = [tuple(literal_eval(v) for v in p.split('\n')) for p in f.read().rstrip().split('\n\n')] def compare(l, r): - if isinstance(l, list): - if isinstance(r, list): + match (l, r): + case ([*l], [*r]): for a, b in zip(l, r): - cmp = compare(a, b) - if cmp != 0: - return cmp + cmp = compare(a, b) + if cmp != 0: return cmp return compare(len(l), len(r)) - else: + case ([*l], int(r)): return compare(l, [r]) - else: - if isinstance(r, list): + case (int(l), [*r]): return compare([l], r) - else: + case (int(l), int(r)): return min(max(l - r, -1), 1) p1 = 0 |