From 963cde3ccda9c7f9a779c4f0ae11a85c613491cd Mon Sep 17 00:00:00 2001 From: Stefan Harmuth Date: Sat, 18 Dec 2021 10:49:16 +0100 Subject: [PATCH] day18: don't double-search --- day18.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/day18.py b/day18.py index 62c8ca1..0dbbb8f 100644 --- a/day18.py +++ b/day18.py @@ -17,7 +17,7 @@ class Snailfish: while self.reduce(): pass - def __add__(self, other): + def __add__(self, other: 'Snailfish'): return Snailfish("[%s,%s]" % (self.pairs, other.pairs)) def reduce(self): @@ -65,8 +65,8 @@ class Snailfish: def getMagnitude(self) -> int: pairs = self.pairs - while re_pair.search(pairs): - for num1, num2 in re_pair.findall(pairs): + while match := re_pair.findall(pairs): + for num1, num2 in match: pairs = pairs.replace("[%s,%s]" % (num1, num2), str(int(num1) * 3 + int(num2) * 2)) return int(pairs)