You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

100 lines
3.0 KiB

#!/usr/bin/env python3
import sys, math
from functools import reduce
from copy import deepcopy
def magn(n):
if type(n) == int: return n
return 3*magn(n[0]) + 2*magn(n[1])
def explode(osn):
sn = deepcopy(osn)
l = None
r = 0
d = False
for i,n1 in enumerate(sn):
if type(n1) == int:
l = (0,i,n1)
if r != 0:
sn[i] = n1 + r
return sn
else:
for j,n2 in enumerate(n1):
if type(n2) == int:
l = (1,[i,j],n2)
if r != 0:
n1[j] = n2 + r
return sn
else:
for k,n3 in enumerate(n2):
if type(n3) == int:
l = (2,[i,j,k],n3)
if r != 0:
n2[k] = n3 + r
return sn
else:
for m,n4 in enumerate(n3):
if type(n4) == int:
l = (3,[i,j,k,m],n4)
if r != 0:
n3[m] = n4 + r
return sn
elif not d:
d = True
r = n4[1]
if l != None:
if l[0] == 0: sn[l[1]] = l[2] + n4[0]
elif l[0] == 1: sn[l[1][0]][l[1][1]] = l[2] + n4[0]
elif l[0] == 2: sn[l[1][0]][l[1][1]][l[1][2]] = l[2] + n4[0]
elif l[0] == 3: sn[l[1][0]][l[1][1]][l[1][2]][l[1][3]] = l[2] + n4[0]
n3[m] = 0
else:
if type(n4[0]) != int:
print('help!')
n4[0] += r
return sn
return sn
def split(osn):
sn = deepcopy(osn)
if isinstance(sn,list):
for i,n in enumerate(sn):
o = sn[i]
sn[i] = split(n)
if sn[i] != o: return sn
elif sn > 9:
return [math.floor(sn/2),math.ceil(sn/2)]
return sn
def snreduce(sn):
new = 0
old = sn
while old != new:
old = new if new != 0 else old
new = explode(old)
if new != old:
continue
new = split(old)
return new
def snadd(sn1,sn2):
sn = [sn1,sn2]
return snreduce(sn)
if __name__ == '__main__':
nums = [eval(line.strip('\n')) for line in open(sys.argv[1])]
# challenge 1
res1 = str(magn(reduce(snadd,nums)))
print("challenge 1:" + "\n" + res1 + "\n")
# challenge 2
mm = 0
for n in nums:
for m in nums:
mm = max(mm, magn(snadd(n,m)))
res2 = str(mm)
print("challenge 2:" + "\n" + res2 + "\n")