day18: don't double-search

This commit is contained in:
Stefan Harmuth 2021-12-18 10:49:16 +01:00
parent 081eb3302a
commit 963cde3ccd

View File

@ -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)