#! /usr/bin/awk -f # hanoi.awk -- generate hanoi moves # $Id: hanoi.awk,v 1.1 1998/09/06 23:02:37 cdua Exp cdua $ # Carlos Duarte, 980712/980722 # usage: ./hanoi.awk -- -m1 4 use method 1, to solve 4 disks problem # usage: ./hanoi.awk -- -m2 6 use method 2, to solve 6 disks problem # usage: ./hanoi.awk -- -m3 use method 3, to solve 3 (default) disks problem # recursive algorithm (two recursive calls) function h1(n, l, m, r) { if (n>=1) { h1(n-1, l,r,m) printf "move from %d to %d\n", l, r h1(n-1, m,l,r) } } # recursive also, but avoid tail recursion function h2(n, l, m, r, t) { while (n>=1) { h2(n-1, l,r,m) printf "move from %d to %d\n", l, r n=n-1 t = l l = m m = t } } # return the kth smaller, 0 based # the index of x is returned; x[0..2] function select_smaller(x, k, c0,c1,c2,o) { c0 = substr(x[0],1,1)+0; if (!c0) c0 = 9999 c1 = substr(x[1],1,1)+0; if (!c1) c1 = 9999 c2 = substr(x[2],1,1)+0; if (!c2) c2 = 9999 if (c0