diff --git a/day-18/day-18.py b/day-18/day-18.py index 4b59717..e904e90 100644 --- a/day-18/day-18.py +++ b/day-18/day-18.py @@ -72,40 +72,37 @@ def explode(n, lvl=0): (left, right) = n if lvl == 4 and isinstance(left, int) and isinstance(right, int): return True, 0, left, right - else: - res, left, l, r = explode(left, lvl+1) + lvl += 1 + res, left, l, r = explode(left, lvl) + if not res: + res, right, l, r = explode(right, lvl) if not res: - res, right, l, r = explode(right, lvl+1) - if not res: - return False, n, None, None + return False, n, None, None + if l: + if isinstance(left, int): + left = left + l + l = None else: - if l: - if isinstance(left, int): - left = left + l - l = None - else: - res, left = explode_to_left(left, l) - if res: - l = None - return True, (left, right), l, r + res, left = explode_to_left(left, l) + if res: + l = None + return True, (left, right), l, r + if r: + if isinstance(right, int): + right = right + r + r = None else: - if r: - if isinstance(right, int): - right = right + r - r = None - else: - res, right = explode_to_right(right, r) - if res: - r = None - return True, (left, right), l, r + res, right = explode_to_right(right, r) + if res: + r = None + return True, (left, right), l, r def split(n): if isinstance(n, int): if 10 <= n: return True, (n // 2, (n + 1) // 2) - else: - return False, n + return False, n (left, right) = n res, left = split(left) if not res: