PERL   31

simpleHanoi

Guest on 16th August 2022 01:07:34 AM

  1. % simpleHanoi.pl.txt
  2. % Towers of Hanoi puzzle solver in Prolog.
  3.  
  4. % Goal is to move, say, 5 disks from left peg
  5. % to right peg, using the middle as a buffer.
  6.  
  7. go :- solve(4, left, middle, right).
  8.  
  9. solve(N, A,B,C) :- N == 0.
  10. solve(N, A,B,C) :-
  11.   M is N - 1,
  12.   solve(M, A, C, B),
  13.   move(A, C),
  14.   solve(M, B, A, C).
  15.  
  16. move(A, B) :- write('Move disk from '),write(A),write(' to '),write(B),nl.

Raw Paste


Login or Register to edit or fork this paste. It's free.