day18: don't double-search
This commit is contained in:
parent
081eb3302a
commit
963cde3ccd
6
day18.py
6
day18.py
@ -17,7 +17,7 @@ class Snailfish:
|
|||||||
while self.reduce():
|
while self.reduce():
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def __add__(self, other):
|
def __add__(self, other: 'Snailfish'):
|
||||||
return Snailfish("[%s,%s]" % (self.pairs, other.pairs))
|
return Snailfish("[%s,%s]" % (self.pairs, other.pairs))
|
||||||
|
|
||||||
def reduce(self):
|
def reduce(self):
|
||||||
@ -65,8 +65,8 @@ class Snailfish:
|
|||||||
|
|
||||||
def getMagnitude(self) -> int:
|
def getMagnitude(self) -> int:
|
||||||
pairs = self.pairs
|
pairs = self.pairs
|
||||||
while re_pair.search(pairs):
|
while match := re_pair.findall(pairs):
|
||||||
for num1, num2 in re_pair.findall(pairs):
|
for num1, num2 in match:
|
||||||
pairs = pairs.replace("[%s,%s]" % (num1, num2), str(int(num1) * 3 + int(num2) * 2))
|
pairs = pairs.replace("[%s,%s]" % (num1, num2), str(int(num1) * 3 + int(num2) * 2))
|
||||||
|
|
||||||
return int(pairs)
|
return int(pairs)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user