1*3a92d97fSJulio Merino# Towers of Hanoi in sed. 2*3a92d97fSJulio Merino# Ex: 3*3a92d97fSJulio Merino# Run "sed -f hanoi.sed", and enter: 4*3a92d97fSJulio Merino# 5*3a92d97fSJulio Merino# :abcd: : :<CR> 6*3a92d97fSJulio Merino# 7*3a92d97fSJulio Merino# note -- TWO carriage returns were once required, this will output the 8*3a92d97fSJulio Merino# sequence of states involved in moving 4 rings, the largest called "a" and 9*3a92d97fSJulio Merino# the smallest called "d", from the first to the second of three towers, so 10*3a92d97fSJulio Merino# that the rings on any tower at any time are in descending order of size. 11*3a92d97fSJulio Merino# You can start with a different arrangement and a different number of rings, 12*3a92d97fSJulio Merino# say :ce:b:ax: and it will give the shortest procedure for moving them all 13*3a92d97fSJulio Merino# to the middle tower. The rules are: the names of the rings must all be 14*3a92d97fSJulio Merino# lower-case letters, they must be input within 3 fields (representing the 15*3a92d97fSJulio Merino# towers) and delimited by 4 colons, such that the letters within each field 16*3a92d97fSJulio Merino# are in alphabetical order (i.e. rings are in descending order of size). 17*3a92d97fSJulio Merino# 18*3a92d97fSJulio Merino# For the benefit of anyone who wants to figure out the script, an "internal" 19*3a92d97fSJulio Merino# line of the form 20*3a92d97fSJulio Merino# b:0abx:1a2b3 :2 :3x2 21*3a92d97fSJulio Merino# has the following meaning: the material after the three markers :1, :2, 22*3a92d97fSJulio Merino# and :3 represents the three towers; in this case the current set-up is 23*3a92d97fSJulio Merino# ":ab : :x :". The numbers after a, b and x in these fields indicate 24*3a92d97fSJulio Merino# that the next time it gets a chance, it will move a to tower 2, move b 25*3a92d97fSJulio Merino# to tower 3, and move x to tower 2. The string after :0 just keeps track 26*3a92d97fSJulio Merino# of the alphabetical order of the names of the rings. The b at the 27*3a92d97fSJulio Merino# beginning means that it is now dealing with ring b (either about to move 28*3a92d97fSJulio Merino# it, or re-evaluating where it should next be moved to). 29*3a92d97fSJulio Merino# 30*3a92d97fSJulio Merino# Although this version is "limited" to 26 rings because of the size of the 31*3a92d97fSJulio Merino# alphabet, one could write a script using the same idea in which the rings 32*3a92d97fSJulio Merino# were represented by arbitrary [strings][within][brackets], and in place of 33*3a92d97fSJulio Merino# the built-in line of the script giving the order of the letters of the 34*3a92d97fSJulio Merino# alphabet, it would accept from the user a line giving the ordering to be 35*3a92d97fSJulio Merino# assumed, e.g. [ucbvax][decvax][hplabs][foo][bar]. 36*3a92d97fSJulio Merino# 37*3a92d97fSJulio Merino# George Bergman 38*3a92d97fSJulio Merino# Math, UC Berkeley 94720 USA 39*3a92d97fSJulio Merino 40*3a92d97fSJulio Merino# cleaning, diagnostics 41*3a92d97fSJulio Merinos/ *//g 42*3a92d97fSJulio Merino/^$/d 43*3a92d97fSJulio Merino/[^a-z:]/{a\ 44*3a92d97fSJulio MerinoIllegal characters: use only a-z and ":". Try again. 45*3a92d97fSJulio Merinod 46*3a92d97fSJulio Merino} 47*3a92d97fSJulio Merino/^:[a-z]*:[a-z]*:[a-z]*:$/!{a\ 48*3a92d97fSJulio MerinoIncorrect format: use\ 49*3a92d97fSJulio Merino\ : string1 : string2 : string3 :<CR>\ 50*3a92d97fSJulio MerinoTry again. 51*3a92d97fSJulio Merinod 52*3a92d97fSJulio Merino} 53*3a92d97fSJulio Merino/\([a-z]\).*\1/{a\ 54*3a92d97fSJulio MerinoRepeated letters not allowed. Try again. 55*3a92d97fSJulio Merinod 56*3a92d97fSJulio Merino} 57*3a92d97fSJulio Merino# initial formatting 58*3a92d97fSJulio Merinoh 59*3a92d97fSJulio Merinos/[a-z]/ /g 60*3a92d97fSJulio MerinoG 61*3a92d97fSJulio Merinos/^:\( *\):\( *\):\( *\):\n:\([a-z]*\):\([a-z]*\):\([a-z]*\):$/:1\4\2\3:2\5\1\3:3\6\1\2:0/ 62*3a92d97fSJulio Merinos/[a-z]/&2/g 63*3a92d97fSJulio Merinos/^/abcdefghijklmnopqrstuvwxyz/ 64*3a92d97fSJulio Merino:a 65*3a92d97fSJulio Merinos/^\(.\).*\1.*/&\1/ 66*3a92d97fSJulio Merinos/.// 67*3a92d97fSJulio Merino/^[^:]/ba 68*3a92d97fSJulio Merinos/\([^0]*\)\(:0.*\)/\2\1:/ 69*3a92d97fSJulio Merinos/^[^0]*0\(.\)/\1&/ 70*3a92d97fSJulio Merino:b 71*3a92d97fSJulio Merino# outputting current state without markers 72*3a92d97fSJulio Merinoh 73*3a92d97fSJulio Merinos/.*:1/:/ 74*3a92d97fSJulio Merinos/[123]//gp 75*3a92d97fSJulio Merinog 76*3a92d97fSJulio Merino:c 77*3a92d97fSJulio Merino# establishing destinations 78*3a92d97fSJulio Merino/^\(.\).*\1:1/td 79*3a92d97fSJulio Merino/^\(.\).*:1[^:]*\11/s/^\(.\)\(.*\1\([a-z]\).*\)\3./\3\2\31/ 80*3a92d97fSJulio Merino/^\(.\).*:1[^:]*\12/s/^\(.\)\(.*\1\([a-z]\).*\)\3./\3\2\33/ 81*3a92d97fSJulio Merino/^\(.\).*:1[^:]*\13/s/^\(.\)\(.*\1\([a-z]\).*\)\3./\3\2\32/ 82*3a92d97fSJulio Merino/^\(.\).*:2[^:]*\11/s/^\(.\)\(.*\1\([a-z]\).*\)\3./\3\2\33/ 83*3a92d97fSJulio Merino/^\(.\).*:2[^:]*\12/s/^\(.\)\(.*\1\([a-z]\).*\)\3./\3\2\32/ 84*3a92d97fSJulio Merino/^\(.\).*:2[^:]*\13/s/^\(.\)\(.*\1\([a-z]\).*\)\3./\3\2\31/ 85*3a92d97fSJulio Merino/^\(.\).*:3[^:]*\11/s/^\(.\)\(.*\1\([a-z]\).*\)\3./\3\2\32/ 86*3a92d97fSJulio Merino/^\(.\).*:3[^:]*\12/s/^\(.\)\(.*\1\([a-z]\).*\)\3./\3\2\31/ 87*3a92d97fSJulio Merino/^\(.\).*:3[^:]*\13/s/^\(.\)\(.*\1\([a-z]\).*\)\3./\3\2\33/ 88*3a92d97fSJulio Merinobc 89*3a92d97fSJulio Merino# iterate back to find smallest out-of-place ring 90*3a92d97fSJulio Merino:d 91*3a92d97fSJulio Merinos/^\(.\)\(:0[^:]*\([^:]\)\1.*:\([123]\)[^:]*\1\)\4/\3\2\4/ 92*3a92d97fSJulio Merinotd 93*3a92d97fSJulio Merino# move said ring (right, resp. left) 94*3a92d97fSJulio Merinos/^\(.\)\(.*\)\1\([23]\)\(.*:\3[^ ]*\) /\1\2 \4\1\3/ 95*3a92d97fSJulio Merinos/^\(.\)\(.*:\([12]\)[^ ]*\) \(.*\)\1\3/\1\2\1\3\4 / 96*3a92d97fSJulio Merinotb 97*3a92d97fSJulio Merinos/.*/Done! Try another, or end with ^D./p 98*3a92d97fSJulio Merinod 99