Untitled

unknown
python
2 years ago
1.7 kB
3
Indexable
Never
```import math

def tolist(str):
str = str.replace("[", "[ ").replace("]", " ] ").replace(",", " , ").split()
return map(lambda ch: int(ch) if ch.isdigit() else ch, str)

def closest(left, right):
il, ir = -1, -1
for i, ch in reversed(list(enumerate(left))):
if type(ch) == int:
il = i
break
for i, ch in enumerate(right):
if type(ch) == int:
ir = i
break
return (il, ir)

def explode(num):
depth = 0
for i, ch in enumerate(num):
if ch == "[": depth += 1
if ch == "]": depth -= 1
if type(ch) == int and depth > 4:
a, b = num[i], num[i+2]
left, right = num[:i-1], num[i+4:]
il, ir = closest(left, right)
if (il > -1): left[il] += a
if (ir > -1): right[ir] += b
num = left + [0] + right
return explode(num)
return num

def split(num):
num = explode(num)
for i, ch in enumerate(num):
if type(ch) == int and ch > 9:
left, right = num[:i], num[i+1:]
val = int(ch) / 2.0
a, b = int(math.floor(val)), int(math.ceil(val))
num = left + ["["] + [a] + [","] + [b] + ["]"] + right
return split(num)
return num

tostr = lambda num: "".join(map(str, num))
add = lambda a,b: ["["] + a + [","] + b + ["]"]
magnitude = lambda num: eval(tostr(num).replace("[", "(3*").replace(",", "+").replace("]", "*2)"))

max_magn = 0
for a in input:
for b in input: