From ed25f645184821945d1c55e15cff8c93612f330f Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Tue, 13 Dec 2022 10:36:05 +0000 Subject: 13 - use pattern matching --- 13.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/13.py b/13.py index b0acec0..c836bd0 100644 --- a/13.py +++ b/13.py @@ -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 -- cgit v1.2.3-54-g00ecf