2008-12-15
Fun with python
$ time python -c "import sys; sys.stdout.write((lambda size, sizesq, allp, openclose: ((lambda mkbrd, brd, mv, av, d, finished: ((lambda h: h(h, mkbrd(), 0, 0, 0)) (lambda h, b, r, c, v: [lambda: ([lambda: '', lambda: ((lambda nb, nv: d([lambda:h(h, nb, r-2, c-1, nv), lambda:h(h, nb, r-1, c-2, nv), lambda:h(h, nb, r-2, c+1, nv), lambda:h(h, nb, r-1, c+2, nv), lambda:h(h, nb, r+1, c-2, nv), lambda:h(h, nb, r+2, c-1, nv), lambda:h(h, nb, r+1, c+2, nv), lambda:h(h, nb, r+2, c+1, nv)])) (mv(b,r,c,v), v+1))] [av(b,r,c)]()), lambda: brd(b)] [finished(v,r,c)]()))) (lambda: [-1] * sizesq, lambda b: '\n'.join((''.join('%4d' % v for v in b[i:i+size])) for i in range(0,sizesq,size)) + '\n\n', lambda b, r, c, v: b[:r*size+c] + [v] + b[r*size+c+1:], lambda b, r, c: int(r >=0 and r < size and c >= 0 and c < size and b[r*size+c] == -1), [lambda fl: ((lambda y: y(y,fl)) (lambda y, ff: [lambda: '', lambda: (lambda r: [lambda: r, lambda: y(y,ff[1:])][r=='']()) (ff[0]())] [len(ff)>0]())), lambda fl: ''.join(f() for f in fl)][allp], [lambda v,r,c: int(v==sizesq), lambda v,r,c: int(v==sizesq and r==0 and c==0)][openclose] ))) (int(sys.argv[1]), int(sys.argv[1])*int(sys.argv[1]), int(sys.argv[2]), int(sys.argv[3])))" 6 0 1
user 0m16.420s
sys 0m0.010s
0 7 4 13 2 29
5 12 1 30 23 14
8 35 6 3 28 31
11 18 9 24 15 22
34 25 20 17 32 27
19 10 33 26 21 16
real 0m16.462suser 0m16.420s
sys 0m0.010s
arguments: the 6 is the size of the board, 0 means to print the first found rather than all, and the 1 means to look for a closed path rather than an open path.
(Blogger ate earlier version due to bad < and > HTML escaping.)|