project-euler/068_magic_5gon_ring.py

41 lines
1.5 KiB
Python

#!/usr/bin/env python3
def m5gon(x, i, mx):
r = []
m = [[] for _ in range(5)]
for a, b in enumerate(range(i-x-1, (i-x)//2, -1), 1):
if a > mx:
break
elif b > mx or a == x or b == x:
continue
m[0] = [x, a, b]
for ci, c in enumerate(range(i-b-1, 0, -1), 1):
if ci > mx:
break
elif c > mx or len(set([a, b, x, ci, c])) < 5:
continue
m[1] = [ci, b, c]
for di, d in enumerate(range(i-c-1, 0, -1), 1):
if di > mx:
break
elif d > mx or len(set([a, b, x, c, ci, d, di])) < 7:
continue
m[2] = [di, c, d]
for ei, e in enumerate(range(i-d-1, 0, -1), 1):
if ei > mx:
break
elif e > mx or len(set([a, b, x, c, ci, d, di, e, ei, i-e-a])) < 10 or i-e-a > mx:
continue
m[3] = [ei, d, e]
m[4] = [i-e-a, e, a]
st = m.index(sorted(m, key = lambda y: y[0])[0])
m = m[st:] + m[:st]
if m[0][1] < m[0][2]:
m = list(map(lambda y: (y[0], y[2], y[1]), [m[0]] + m[::-1][:-1]))
r.append(''.join([''.join(map(str, y)) for y in m]))
return r
if __name__ == '__main__':
print(max(m5gon(10, i, 10) for i in range(10, 25))[0])