-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrosettacode.org_TowersOfHanoi_AtoZ.txt
2635 lines (2218 loc) · 66.7 KB
/
rosettacode.org_TowersOfHanoi_AtoZ.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
* Towers of Hanoi 08/09/2015
HANOITOW CSECT
USING HANOITOW,R12 r12 : base register
LR R12,R15 establish base register
ST R14,SAVE14 save r14
BEGIN LH R2,=H'4' n <===
L R3,=C'123 ' stating position
BAL R14,MOVE r1=move(m,n)
RETURN L R14,SAVE14 restore r14
BR R14 return to caller
SAVE14 DS F static save r14
PG DC CL44'xxxxxxxxxxxx Move disc from pole X to pole Y'
NN DC F'0'
POLEX DS F current poles
POLEN DS F new poles
* .... recursive subroutine move(n, poles) [r2,r3]
MOVE LR R10,R11 save stackptr (r11) in r10 temp
LA R1,STACKLEN amount of storage required
GETMAIN RU,LV=(R1) allocate storage for stack
USING STACKDS,R11 make storage addressable
LR R11,R1 establish stack addressability
ST R14,SAVE14M save previous r14
ST R10,SAVE11M save previous r11
LR R1,R5 restore saved argument r5
BEGINM STM R2,R3,STACK push arguments to stack
ST R3,POLEX
CH R2,=H'1' if n<>1
BNE RECURSE then goto recurse
L R1,NN
LA R1,1(R1) nn=nn+1
ST R1,NN
XDECO R1,PG nn
MVC PG+33(1),POLEX+0 from
MVC PG+43(1),POLEX+1 to
XPRNT PG,44 print "move disk from to"
B RETURNM
RECURSE L R2,N n
BCTR R2,0 n=n-1
MVC POLEN+0(1),POLES+0 from
MVC POLEN+1(1),POLES+2 via
MVC POLEN+2(1),POLES+1 to
L R3,POLEN new poles
BAL R14,MOVE call move(n-1,from,via,to)
LA R2,1 n=1
MVC POLEN,POLES
L R3,POLEN new poles
BAL R14,MOVE call move(1,from,to,via)
L R2,N n
BCTR R2,0 n=n-1
MVC POLEN+0(1),POLES+2 via
MVC POLEN+1(1),POLES+1 to
MVC POLEN+2(1),POLES+0 from
L R3,POLEN new poles
BAL R14,MOVE call move(n-1,via,to,from)
RETURNM LM R2,R3,STACK pull arguments from stack
LR R1,R11 current stack
L R14,SAVE14M restore r14
L R11,SAVE11M restore r11
LA R0,STACKLEN amount of storage to free
FREEMAIN A=(R1),LV=(R0) free allocated storage
BR R14 return to caller
LTORG
DROP R12 base no longer needed
STACKDS DSECT dynamic area
SAVE14M DS F saved r14
SAVE11M DS F saved r11
STACK DS 0F stack
N DS F r2 n
POLES DS F r3 poles
STACKLEN EQU *-STACKDS
YREGS
END HANOITOW
5 var, disks
var sa
var sb
var sc
: save sc ! sb ! sa ! disks ! ;
: get sa @ sb @ sc @ ;
: get2 get swap ;
: hanoi
save disks @ not if ;; then
disks @ get
disks @ n:1- get2 hanoi save
cr
" move a ring from " . sa @ . " to " . sb @ .
disks @ n:1- get2 rot hanoi
;
" Tower of Hanoi, with " . disks @ . " rings: " .
disks @ 1 2 3 hanoi cr bye
public function move(n:int, from:int, to:int, via:int):void
{
if (n > 0)
{
move(n - 1, from, via, to);
trace("Move disk from pole " + from + " to pole " + to);
move(n - 1, via, to, from);
}
}
with Ada.Text_Io; use Ada.Text_Io;
procedure Towers is
type Pegs is (Left, Center, Right);
procedure Hanoi (Ndisks : Natural; Start_Peg : Pegs := Left; End_Peg : Pegs := Right; Via_Peg : Pegs := Center) is
begin
if Ndisks > 0 then
Hanoi(Ndisks - 1, Start_Peg, Via_Peg, End_Peg);
Put_Line("Move disk" & Natural'Image(Ndisks) & " from " & Pegs'Image(Start_Peg) & " to " & Pegs'Image(End_Peg));
Hanoi(Ndisks - 1, Via_Peg, End_Peg, Start_Peg);
end if;
end Hanoi;
begin
Hanoi(4);
end Towers;
move := proc(n::number, src::number, dst::number, via::number) is
if n > 0 then
move(n - 1, src, via, dst)
print(src & ' to ' & dst)
move(n - 1, via, dst, src)
fi
end
move(4, 1, 2, 3)
PROC move = (INT n, from, to, via) VOID:
IF n > 0 THEN
move(n - 1, from, via, to);
printf(($"Move disk from pole "g" to pole "gl$, from, to));
move(n - 1, via, to, from)
FI
;
main: (
move(4, 1,2,3)
)
begin
procedure move ( integer value n, from, to, via ) ;
if n > 0 then begin
move( n - 1, from, via, to );
write( i_w := 1, s_w := 0, "Move disk from peg: ", from, " to peg: ", to );
move( n - 1, via, to, from )
end move ;
move( 4, 1, 2, 3 )
end.
PROC move(n, from, to, via)
IF n > 0
move(n-1, from, via, to)
WriteF('Move disk from pole \d to pole \d\n', from, to)
move(n-1, via, to, from)
ENDIF
ENDPROC
PROC main()
move(4, 1,2,3)
ENDPROC
global moves --this is so the handler 'hanoi' can see the 'moves' variable
set moves to ""
hanoi(4, "peg A", "peg C", "peg B")
on hanoi(ndisks, fromPeg, toPeg, withPeg)
if ndisks is greater than 0 then
hanoi(ndisks - 1, fromPeg, withPeg, toPeg)
set moves to moves & "Move disk " & ndisks & " from " & fromPeg & " to " & toPeg & return
hanoi(ndisks - 1, withPeg, toPeg, fromPeg)
end if
return moves
end hanoi
on run
map(arrows, hanoi(3, "left", "right", "mid"))
-- {"left -> right", "left -> mid", "right -> mid",
-- "left -> right", "mid -> left", "mid -> right", "left -> right"}
end run
-- n -> s -> s -> s -> [(s, s)]
on hanoi(n, a, b, c)
if n > 0 then
hanoi(n - 1, a, c, b) & {{a, b}} & hanoi(n - 1, c, b, a)
else
{}
end if
end hanoi
-- DISPLAY FUNCTION
-- (a, a) -> String
on arrows(x)
item 1 of x & " -> " & item 2 of x
end arrows
-- LIBRARY FUNCTION
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
script mf
property lambda : f
end script
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to mf's lambda(item i of xs, i, xs)
end repeat
return lst
end map
move(n, from, to, via) ;n = # of disks, from = start pole, to = end pole, via = remaining pole
{
if (n = 1)
{
msgbox , Move disk from pole %from% to pole %to%
}
else
{
move(n-1, from, via, to)
move(1, from, to, via)
move(n-1, via, to, from)
}
}
move(64, 1, 3, 2)
Func move($n, $from, $to, $via)
If ($n = 1) Then
ConsoleWrite(StringFormat("Move disk from pole "&$from&" To pole "&$to&"\n"))
Else
move($n - 1, $from, $via, $to)
move(1, $from, $to, $via)
move($n - 1, $via, $to, $from)
EndIf
EndFunc
move(4, 1,2,3)
$ awk 'func hanoi(n,f,t,v){if(n>0){hanoi(n-1,f,v,t);print(f,"->",t);hanoi(n-1,v,t,f)}}
BEGIN{hanoi(4,"left","middle","right")}'
SUB move (n AS Integer, fromPeg AS Integer, toPeg AS Integer, viaPeg AS Integer)
IF n>0 THEN
move n-1, fromPeg, viaPeg, toPeg
PRINT "Move disk from "; fromPeg; " to "; toPeg
move n-1, viaPeg, toPeg, fromPeg
END IF
END SUB
move 4,1,2,3
10 DIM N(1024), F(1024), T(1024), V(1024): REM STACK PER PARAMETER
20 SP = 0: REM STACK POINTER
30 N(SP) = 4: REM START WITH 4 DISCS
40 F(SP) = 1: REM ON PEG 1
50 T(SP) = 2: REM MOVE TO PEG 2
60 V(SP) = 3: REM VIA PEG 3
70 GOSUB 100
80 END
90 REM MOVE SUBROUTINE
100 IF N(SP) = 0 THEN RETURN
110 OS = SP: REMEMBER STACK POINTER
120 SP = SP + 1: REM INCREMENT STACK POINTER
130 N(SP) = N(OS) - 1: REM MOVE N-1 DISCS
140 F(SP) = F(OS) : REM FROM START PEG
150 T(SP) = V(OS) : REM TO VIA PEG
160 V(SP) = T(OS) : REM VIA TO PEG
170 GOSUB 100
180 OS = SP - 1: REM OS WILL HAVE CHANGED
190 PRINT "MOVE DISC FROM"; F(OS); "TO"; T(OS)
200 N(SP) = N(OS) - 1: REM MOVE N-1 DISCS
210 F(SP) = V(OS) : REM FROM VIA PEG
220 T(SP) = T(OS) : REM TO DEST PEG
230 V(SP) = F(OS) : REM VIA FROM PEG
240 GOSUB 100
250 SP = SP - 1 : REM RESTORE STACK POINTER FOR CALLER
260 RETURN
call move(4,1,2,3)
print "Towers of Hanoi puzzle completed!"
end
subroutine move (n, fromPeg, toPeg, viaPeg)
if n>0 then
call move(n-1, fromPeg, viaPeg, toPeg)
print "Move disk from "+fromPeg+" to "+toPeg
call move(n-1, viaPeg, toPeg, fromPeg)
end if
end subroutine
@echo off
setlocal enabledelayedexpansion
%==The main thing==%
%==First param - Number of disks==%
%==Second param - Start pole==%
%==Third param - End pole==%
%==Fourth param - Helper pole==%
call :move 4 START END HELPER
echo.
pause
exit /b 0
%==The "function"==%
:move
setlocal
set n=%1
set from=%2
set to=%3
set via=%4
if %n% gtr 0 (
set /a x=!n!-1
call :move !x! %from% %via% %to%
echo Move top disk from pole %from% to pole %to%.
call :move !x! %via% %to% %from%
)
exit /b 0
DIM Disc$(13),Size%(3)
FOR disc% = 1 TO 13
Disc$(disc%) = STRING$(disc%," ")+STR$disc%+STRING$(disc%," ")
IF disc%>=10 Disc$(disc%) = MID$(Disc$(disc%),2)
Disc$(disc%) = CHR$17+CHR$(128+disc%-(disc%>7))+Disc$(disc%)+CHR$17+CHR$128
NEXT disc%
MODE 3
OFF
ndiscs% = 13
FOR n% = ndiscs% TO 1 STEP -1
PROCput(n%,1)
NEXT
INPUT TAB(0,0) "Press Enter to start" dummy$
PRINT TAB(0,0) SPC(20);
PROChanoi(ndiscs%,1,2,3)
VDU 30
END
DEF PROChanoi(a%,b%,c%,d%)
IF a%=0 ENDPROC
PROChanoi(a%-1,b%,d%,c%)
PROCtake(a%,b%)
PROCput(a%,c%)
PROChanoi(a%-1,d%,c%,b%)
ENDPROC
DEF PROCput(disc%,peg%)
PRINTTAB(13+26*(peg%-1)-disc%,20-Size%(peg%))Disc$(disc%);
Size%(peg%) = Size%(peg%)+1
ENDPROC
DEF PROCtake(disc%,peg%)
Size%(peg%) = Size%(peg%)-1
PRINTTAB(13+26*(peg%-1)-disc%,20-Size%(peg%))STRING$(2*disc%+1," ");
ENDPROC
( ( move
= n from to via
. !arg:(?n,?from,?to,?via)
& ( !n:>0
& move$(!n+-1,!from,!via,!to)
& out$("Move disk from pole " !from " to pole " !to)
& move$(!n+-1,!via,!to,!from)
|
)
)
& move$(4,1,2,3)
);
[
This implementation is recursive and uses
a stack, consisting of frames that are 8
bytes long. The layout is as follows:
Byte Description
0 recursion flag
(the program stops if the flag is
zero)
1 the step which is currently
executed
4 means a call to
move(a, c, b, n - 1)
3 means a call to
move(a, b, c, 1)
2 means a call to
move(b, a, c, n - 1)
1 prints the source and dest pile
2 flag to check whether the current
step has already been done or if
it still must be executed
3 the step which will be executed
in the next loop
4 the source pile
5 the helper pile
6 the destination pile
7 the number of disks to move
The first stack frame (0 0 0 0 0 0 0 0)
is used to abort the recursion.
]
>>>>>>>>
These are the parameters for the program
(1 4 1 0 'a 'b 'c 5)
+>++++>+>>
>>>>++++++++[<++++++++++++>-]<
[<<<+>+>+>-]<<<+>++>+++>+++++>
<<<<<<<<
[> while (recurse)
[- if (step gt 0)
>[-]+< todo = 1
[- if (step gt 1)
[- if (step gt 2)
[- if (step gt 3)
>>+++<< next = 3
>-< todo = 0
>>>>>>[>+>+<<-]>[<+>-]> n dup
-
[[-] if (sub(n 1) gt 0)
<+>>>++++> push (1 0 0 4)
copy and push a
<<<<<<<<[>>>>>>>>+>+
<<<<<<<<<-]>>>>>>>>
>[<<<<<<<<<+>>>>>>>>>-]< >
copy and push c
<<<<<<<[>>>>>>>+>+
<<<<<<<<-]>>>>>>>
>[<<<<<<<<+>>>>>>>>-]< >
copy and push b
<<<<<<<<<[>>>>>>>>>+>+
<<<<<<<<<<-]>>>>>>>>>
>[<<<<<<<<<<+>>>>>>>>>>-]< >
copy n and push sub(n 1)
<<<<<<<<[>>>>>>>>+>+
<<<<<<<<<-]>>>>>>>>
>[<<<<<<<<<+>>>>>>>>>-]< -
>>
]
<<<<<<<<
]
>[-< if ((step gt 2) and todo)
>>++<< next = 2
>>>>>>>
+>>>+> push 1 0 0 1 a b c 1
<<<<<<<<[>>>>>>>>+>+
<<<<<<<<<-]>>>>>>>>
>[<<<<<<<<<+>>>>>>>>>-]< > a
<<<<<<<<[>>>>>>>>+>+
<<<<<<<<<-]>>>>>>>>
>[<<<<<<<<<+>>>>>>>>>-]< > b
<<<<<<<<[>>>>>>>>+>+
<<<<<<<<<-]>>>>>>>>
>[<<<<<<<<<+>>>>>>>>>-]< > c
+ >>
>]<
]
>[-< if ((step gt 1) and todo)
>>>>>>[>+>+<<-]>[<+>-]> n dup
-
[[-] if (n sub 1 gt 0)
<+>>>++++> push (1 0 0 4)
copy and push b
<<<<<<<[>>>>>>>+
<<<<<<<-]>>>>>>>
>[<<<<<<<<+>>>>>>>>-]< >
copy and push a
<<<<<<<<<[>>>>>>>>>+
<<<<<<<<<-]>>>>>>>>>
>[<<<<<<<<<<+>>>>>>>>>>-]< >
copy and push c
<<<<<<<<[>>>>>>>>+
<<<<<<<<-]>>>>>>>>
>[<<<<<<<<<+>>>>>>>>>-]< >
copy n and push sub(n 1)
<<<<<<<<[>>>>>>>>+>+
<<<<<<<<<-]>>>>>>>>
>[<<<<<<<<<+>>>>>>>>>-]< -
>>
]
<<<<<<<<
>]<
]
>[-< if ((step gt 0) and todo)
>>>>>>>
>++++[<++++++++>-]<
>>++++++++[<+++++++++>-]<++++
>>++++++++[<++++++++++++>-]<+++++
>>+++++++++[<++++++++++++>-]<+++
<<<
>.+++++++>.++.--.<<.
>>-.+++++.----.<<.
>>>.<---.+++.>+++.+.+.<.<<.
>.>--.+++++.---.++++.
-------.+++.<<.
>>>++.-------.-.<<<.
>+.>>+++++++.---.-----.<<<.
<<<<.>>>>.
>>----.>++++++++.<+++++.<<.
>.>>.---.-----.<<<.
<<.>>++++++++++++++.
>>>[-]<[-]<[-]<[-]
+++++++++++++.---.[-]
<<<<<<<
>]<
>>[<<+>>-]<< step = next
]
return with clear stack frame
<[-]>[-]>[-]>[-]>[-]>[-]>[-]>[-]<<<<<<
<<<<<<<<
>>[<<+>>-]<< step = next
<
]
#include <stdio.h>
void move(int n, int from, int to, int via)
{
if (n > 0) {
move(n - 1, from, via, to);
printf("Move disk from pole %d to pole %d\n", from, to);
move(n - 1, via, to, from);
}
}
int main()
{
move(4, 1,2,3);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
typedef struct { int *x, n; } tower;
tower *new_tower(int cap)
{
tower *t = calloc(1, sizeof(tower) + sizeof(int) * cap);
t->x = (int*)(t + 1);
return t;
}
tower *t[3];
int height;
void text(int y, int i, int d, const char *s)
{
printf("\033[%d;%dH", height - y + 1, (height + 1) * (2 * i + 1) - d);
while (d--) printf("%s", s);
}
void add_disk(int i, int d)
{
t[i]->x[t[i]->n++] = d;
text(t[i]->n, i, d, "==");
usleep(100000);
fflush(stdout);
}
int remove_disk(int i)
{
int d = t[i]->x[--t[i]->n];
text(t[i]->n + 1, i, d, " ");
return d;
}
void move(int n, int from, int to, int via)
{
if (!n) return;
move(n - 1, from, via, to);
add_disk(to, remove_disk(from));
move(n - 1, via, to, from);
}
int main(int c, char *v[])
{
puts("\033[H\033[J");
if (c <= 1 || (height = atoi(v[1])) <= 0)
height = 8;
for (c = 0; c < 3; c++) t[c] = new_tower(height);
for (c = height; c; c--) add_disk(0, c);
move(height, 0, 2, 1);
text(1, 0, 1, "\n");
return 0;
}
public void move(int n, int from, int to, int via) {
if (n == 1) {
System.Console.WriteLine("Move disk from pole " + from + " to pole " + to);
} else {
move(n - 1, from, via, to);
move(1, from, to, via);
move(n - 1, via, to, from);
}
}
void move(int n, int from, int to, int via) {
if (n == 1) {
std::cout << "Move disk from pole " << from << " to pole " << to << std::endl;
} else {
move(n - 1, from, via, to);
move(1, from, to, via);
move(n - 1, via, to, from);
}
}
(defn towers-of-hanoi [n from to via]
(if (= n 1)
(println (format "Move from %s to %s" from to))
(do
(towers-of-hanoi (dec n) from via to)
(println (format "Move from %s to %s" from to))
(recur (dec n) via to from))))
>>SOURCE FREE
IDENTIFICATION DIVISION.
PROGRAM-ID. towers-of-hanoi.
PROCEDURE DIVISION.
CALL "move-disk" USING 4, 1, 2, 3
.
END PROGRAM towers-of-hanoi.
IDENTIFICATION DIVISION.
PROGRAM-ID. move-disk RECURSIVE.
DATA DIVISION.
LINKAGE SECTION.
01 n PIC 9 USAGE COMP.
01 from-pole PIC 9 USAGE COMP.
01 to-pole PIC 9 USAGE COMP.
01 via-pole PIC 9 USAGE COMP.
PROCEDURE DIVISION USING n, from-pole, to-pole, via-pole.
IF n > 0
SUBTRACT 1 FROM n
CALL "move-disk" USING CONTENT n, from-pole, via-pole, to-pole
DISPLAY "Move disk from pole " from-pole " to pole " to-pole
CALL "move-disk" USING CONTENT n, via-pole, to-pole, from-pole
END-IF
.
END PROGRAM move-disk.
hanoi = (ndisks, start_peg=1, end_peg=3) ->
if ndisks
staging_peg = 1 + 2 + 3 - start_peg - end_peg
hanoi(ndisks-1, start_peg, staging_peg)
console.log "Move disk #{ndisks} from peg #{start_peg} to #{end_peg}"
hanoi(ndisks-1, staging_peg, end_peg)
hanoi(4)
(defun move (n from to via)
(cond ((= n 1)
(format t "Move from ~A to ~A.~%" from to))
(t
(move (- n 1) from via to)
(format t "Move from ~A to ~A.~%" from to)
(move (- n 1) via to from))))
import std.stdio;
void hanoi(in int n, in char from, in char to, in char via) {
if (n > 0) {
hanoi(n - 1, from, via, to);
writefln("Move disk %d from %s to %s", n, from, to);
hanoi(n - 1, via, to, from);
}
}
void main() {
hanoi(3, 'L', 'M', 'R');
}
// Code found and then improved by Glenn C. Rhoads,
// then some more by M. Kolar (2000).
void main(in string[] args) {
import core.stdc.stdio, std.conv, std.typetuple;
immutable size_t n = (args.length > 1) ? args[1].to!size_t : 3;
size_t[3] p = [(1 << n) - 1, 0, 0];
// Show the start configuration of the pegs.
'|'.putchar;
foreach_reverse (immutable i; 1 .. n + 1)
printf(" %d", i);
"\n|\n|".puts;
foreach (immutable size_t x; 1 .. (1 << n)) {
{
immutable size_t i1 = x & (x - 1);
immutable size_t fr = (i1 + i1 / 3) & 3;
immutable size_t i2 = (x | (x - 1)) + 1;
immutable size_t to = (i2 + i2 / 3) & 3;
size_t j = 1;
for (size_t w = x; !(w & 1); w >>= 1, j <<= 1) {}
// Now j is not the number of the disk to move,
// it contains the single bit to be moved:
p[fr] &= ~j;
p[to] |= j;
}
// Show the current configuration of pegs.
foreach (immutable size_t k; TypeTuple!(0, 1, 2)) {
"\n|".printf;
size_t j = 1 << n;
foreach_reverse (immutable size_t w; 1 .. n + 1) {
j >>= 1;
if (j & p[k])
printf(" %zd", w);
}
}
'\n'.putchar;
}
}
main() {
moveit(from,to) {
print("move ${from} ---> ${to}");
}
hanoi(height,toPole,fromPole,usePole) {
if (height>0) {
hanoi(height-1,usePole,fromPole,toPole);
moveit(fromPole,toPole);
hanoi(height-1,toPole,usePole,fromPole);
}
}
hanoi(3,3,1,2);
}
main() {
String say(String from, String to) => "$from ---> $to";
hanoi(int height, int toPole, int fromPole, int usePole) {
if (height > 0) {
hanoi(height - 1, usePole, fromPole, toPole);
print(say(fromPole.toString(), toPole.toString()));
hanoi(height - 1, toPole, usePole, fromPole);
}
}
hanoi(3, 3, 1, 2);
}
[ # move(from, to)
n # print from
[ --> ]n # print " --> "
p # print to\n
sw # p doesn't pop, so get rid of the value
]sm
[ # init(n)
sw # tuck n away temporarily
9 # sentinel as bottom of stack
lw # bring n back
1 # "from" tower's label
3 # "to" tower's label
0 # processed marker
]si
[ # Move()
lt # push to
lf # push from
lmx # call move(from, to)
]sM
[ # code block <d>
ln # push n
lf # push from
lt # push to
1 # push processed marker 1
ln # push n
1 # push 1
- # n - 1
lf # push from
ll # push left
0 # push processed marker 0
]sd
[ # code block <e>
ln # push n
1 # push 1
- # n - 1
ll # push left
lt # push to
0 # push processed marker 0
]se
[ # code block <x>
ln 1 =M
ln 1 !=d
]sx
[ # code block <y>
lMx
lex
]sy
[ # quit()
q # exit the program
]sq
[ # run()
d 9 =q # if stack empty, quit()
sp # processed
st # to
sf # from
sn # n
6 #
lf #
- #
lt #
- # 6 - from - to
sl #
lp 0 =x #
lp 0 !=y #
lrx # loop
]sr
5lix # init(n)
lrx # run()
def move(out, n, fromPeg, toPeg, viaPeg) {
if (n.aboveZero()) {
move(out, n.previous(), fromPeg, viaPeg, toPeg)
out.println(`Move disk $n from $fromPeg to $toPeg.`)
move(out, n.previous(), viaPeg, toPeg, fromPeg)
}
}
move(stdout, 4, def left {}, def right {}, def middle {})
class
APPLICATION
create
make
feature {NONE} -- Initialization
make
do
move (4, "A", "B", "C")
end
feature -- Towers of Hanoi
move (n: INTEGER; frm, to, via: STRING)
require
n > 0
do
if n = 1 then
print ("Move disk from pole " + frm + " to pole " + to + "%N")
else
move (n - 1, frm, via, to)
move (1, frm, to, via)
move (n - 1, via, to, frm)
end
end
end
open monad io
:::IO
//Functional approach
hanoi 0 _ _ _ = []
hanoi n a b c = hanoi (n - 1) a c b ++ [(a,b)] ++ hanoi (n - 1) c b a
hanoiIO n = mapM_ f $ hanoi n 1 2 3 where
f (x,y) = putStrLn $ "Move " ++ show x ++ " to " ++ show y
//Imperative approach using IO monad
hanoiM n = hanoiM' n 1 2 3 where
hanoiM' 0 _ _ _ = return ()
hanoiM' n a b c = do
hanoiM' (n - 1) a c b
putStrLn $ "Move " ++ show a ++ " to " ++ show b
hanoiM' (n - 1) c b a
defmodule RC do
def hanoi(n) when 0<n and n<10, do: hanoi(n, 1, 2, 3)
defp hanoi(1, f, _, t), do: move(f, t)
defp hanoi(n, f, u, t) do
hanoi(n-1, f, t, u)
move(f, t)
hanoi(n-1, u, f, t)
end
defp move(f, t), do: IO.puts "Move disk from #{f} to #{t}"
end
RC.hanoi(3)
(defun move (n from to via)
(cond ((= n 1)
(print (format "Move from %S to %S" from to)))
(t
(progn
(move (- n 1) from via to)
(print (format "Move from %S to %S" from to))
(move (- n 1) via to from)))))
move(1, F, T, _V) ->
io:format("Move from ~p to ~p~n", [F, T]);
move(N, F, T, V) ->
move(N-1, F, V, T),
move(1 , F, T, V),
move(N-1, V, T, F).
!-----------------------------------------------------------
! HANOI.R : solve tower of Hanoi puzzle using a recursive
! modified algorithm.
!-----------------------------------------------------------
PROGRAM HANOI
!$INTEGER
!VAR I,J,MOSSE,NUMBER
PROCEDURE PRINTMOVE
LOCAL SOURCE$,DEST$
MOSSE=MOSSE+1
CASE I OF
1-> SOURCE$="Left" END ->
2-> SOURCE$="Center" END ->
3-> SOURCE$="Right" END ->
END CASE
CASE J OF
1-> DEST$="Left" END ->
2-> DEST$="Center" END ->
3-> DEST$="Right" END ->
END CASE
PRINT("I move a disk from ";SOURCE$;" to ";DEST$)
END PROCEDURE
PROCEDURE MOVE
IF NUMBER<>0 THEN
NUMBER=NUMBER-1
J=6-I-J
MOVE
J=6-I-J
PRINTMOVE
I=6-I-J
MOVE
I=6-I-J
NUMBER=NUMBER+1
END IF
END PROCEDURE
BEGIN
MAXNUM=12
MOSSE=0
PRINT(CHR$(12);TAB(25);"--- TOWERS OF HANOI ---")
REPEAT
PRINT("Number of disks ";)
INPUT(NUMBER)
UNTIL NUMBER>1 AND NUMBER<=MAXNUM
PRINT
PRINT("For ";NUMBER;"disks the total number of moves is";2^NUMBER-1)
I=1 ! number of source pole
J=3 ! number of destination pole
MOVE
END PROGRAM
#light
let rec hanoi num start finish =
match num with
| 0 -> [ ]
| _ -> let temp = (6 - start - finish)
(hanoi (num-1) start temp) @ [ start, finish ] @ (hanoi (num-1) temp finish)
[<EntryPoint>]
let main args =