diff options
author | Tomasz Kramkowski <tk@the-tk.com> | 2021-12-18 23:48:33 +0000 |
---|---|---|
committer | Tomasz Kramkowski <tk@the-tk.com> | 2021-12-18 23:48:33 +0000 |
commit | 2a84108f1223acfaa1680995d02686f2260912b2 (patch) | |
tree | 1a2780d96c67e5afeb3f5d61f64a09b4989bf5a8 | |
parent | 89ffc53ee9f67e509452ea5ac4683d94a62d8c4a (diff) | |
download | aoc2021-2a84108f1223acfaa1680995d02686f2260912b2.tar.gz aoc2021-2a84108f1223acfaa1680995d02686f2260912b2.tar.xz aoc2021-2a84108f1223acfaa1680995d02686f2260912b2.zip |
day 18: now concise-er
-rw-r--r-- | 18.py | 17 |
1 files changed, 8 insertions, 9 deletions
@@ -41,15 +41,14 @@ def try_explode(n, depth=0): return val if not is_pair(n): return None if depth == 4: return n - car_val = try_explode(n.car, depth + 1) - if car_val is not None: - if depth == 3: n.car = 0 - return Pair(car_val.car, try_add(n, car_val.cdr, cdr, car, cdr)) - else: - cdr_val = try_explode(n.cdr, depth + 1) - if cdr_val is not None: - if depth == 3: n.cdr = 0 - return Pair(try_add(n, cdr_val.car, car, cdr, car), cdr_val.cdr) + for access in (car, cdr): + val = try_explode(access(n), depth + 1) + if val is None: continue + if depth == 3: access(n, 0) + if access == car: + return Pair(val.car, try_add(n, val.cdr, cdr, car, cdr)) + else: + return Pair(try_add(n, val.car, car, cdr, car), val.cdr) tests = [ ('[[[[[9,8],1],2],3],4]', '[[[[0,9],2],3],4]'), |