kernelthread.com

The Towers Of Hanoi in sed

The Towers of Hanoi in sed

The script can be tried as follows (assuming you saved it to a file called hanoi.sed:

% echo xxx | sed -f hanoi.sed

The above command would solve a puzzle for 3 disks (hence the 3 'x's). Click here to download the script, which should work with GNU sed or any robust implementation of sed. In case the script causes sed to choke (for example, on Solaris, sed might complain about "labels being too long"), try the differently formatted version here.

Finally, consider the "beautified" version below:

s~^xx*$~:n:3:2:1:&:~;tB;d;:B;/^:$/d;h s~^:.:\(.\):.:\(.\):*:.*~\2 --> \1~;x /^:y:.:.:.:*:.*/b0;/^:n:.:.:.:x:.*/b1 s~:n:\(.\):\(.\):\(.:x*\)x:\(.*\)~:n:\2:\1:\3:y:\1:\2:\3x:\4~ bB;:1;x;p;x;s~^:n:.:.:.:x:\(.*\)~:\1~;bB;:0;x;p;x s~^:y:\(.\):\(.\):\(.\):x\(x*:*\)~:n:\1:\3:\2:\4~ bB