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