def brute_force(): count = 0 from permut import couple for n in range(N,1,-1): for c in couple(integers,n): if reduce(lambda x,y: x+int(y),c, 0) == N: #print c count += 1 return count
def solve(): # polygon = 3 # first_digit = 4 # r = filter(lambda x: x!=first_digit, range(1,7)) polygon = 5 first_digit = 10 r = filter(lambda x: x!=first_digit, range(1,11)) matchs = [] for c in couple(r, 2, optimized=False): if c[0] == c[1]: continue c = map(int, c) c.insert(0, first_digit) glines = [[c]] for lines in glines: rr = filter(lambda x: x not in reduce(lambda x,y: x+y, lines), r) if len(lines) == polygon-1: last = [rr[0], lines[-1][-1], lines[0][1]] if sum(last) == sum(lines[0]): lines.append(last) matchs.append(lines[:]) print "find", lines continue for cc in couple(rr, 2, optimized=False): if cc[0] == cc[1]: continue cc = map(int, cc) cc.insert(1, lines[-1][-1]) if sum(cc) != sum(lines[0]): continue newlines = lines[:] newlines.append(cc) glines.append(newlines) # "sort" the lines by turning cyclicly until the first element of the first line has the minimal value matchs_sorted = [] for lines in matchs: index = lines.index(min(lines)) matchs_sorted.append( lines[index:] + lines[:index] ) return max([int(''.join(map(str, reduce(lambda x,y:x+y, m)))) for m in matchs_sorted])