Advertisement
Guest User

Untitled

a guest
May 20th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 2.05 KB | None | 0 0
  1. (define (hanoi discs)
  2.   (define (helper d direction currentState moveCount smallPosition solution)
  3.     (display currentState) (newline)
  4.     (cond
  5.       [(>= moveCount (sub1 (expt 2 d))) (reverse solution)]
  6.       [(= (modulo moveCount 2) 0)
  7.        (helper d direction
  8.                (list-update
  9.                 (list-update currentState smallPosition cdr)
  10.                 (modulo (+ smallPosition direction) 3)
  11.                 (λ (x) (cons d x)))
  12.                (add1 moveCount)
  13.                (modulo (+ smallPosition direction) 3)
  14.                (cons (list smallPosition (modulo (+ smallPosition direction) 3)) solution))]
  15.       [(> (car (list-ref currentState (car (remove smallPosition (list 0 1 2)))))
  16.           (car (list-ref currentState (cadr (remove smallPosition (list 0 1 2))))))
  17.        (helper d direction
  18.                (list-update
  19.                 (list-update currentState (car (remove smallPosition (list 0 1 2))) cdr)
  20.                 (cadr (remove smallPosition (list 0 1 2)))
  21.                 (λ (x) (cons (car (list-ref currentState (car (remove smallPosition (list 0 1 2))))) x)))
  22.                (add1 moveCount)
  23.                smallPosition
  24.                (cons (list (car (remove smallPosition (list 0 1 2))) (cadr (remove smallPosition (list 0 1 2)))) solution))]
  25.       [else (helper d direction
  26.                     (list-update
  27.                      (list-update currentState (cadr (remove smallPosition (list 0 1 2))) cdr)
  28.                      (car (remove smallPosition (list 0 1 2)))
  29.                      (λ (x) (cons (car (list-ref currentState (cadr (remove smallPosition (list 0 1 2))))) x)))
  30.                     (add1 moveCount)
  31.                     smallPosition
  32.                     (cons (list (cadr (remove smallPosition (list 0 1 2))) (car (remove smallPosition (list 0 1 2)))) solution))]))
  33.   (if (= (modulo discs 2) 0)
  34.       (helper discs 1 (list (reverse (build-list (add1 discs) values)) (list 0) (list 0)) 0 0 empty)
  35.       (helper discs -1 (list (reverse (build-list (add1 discs) values)) (list 0) (list 0)) 0 0 empty)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement