Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (define (hanoi discs)
- (define (helper d direction currentState moveCount smallPosition solution)
- (display currentState) (newline)
- (cond
- [(>= moveCount (sub1 (expt 2 d))) (reverse solution)]
- [(= (modulo moveCount 2) 0)
- (helper d direction
- (list-update
- (list-update currentState smallPosition cdr)
- (modulo (+ smallPosition direction) 3)
- (λ (x) (cons d x)))
- (add1 moveCount)
- (modulo (+ smallPosition direction) 3)
- (cons (list smallPosition (modulo (+ smallPosition direction) 3)) solution))]
- [(> (car (list-ref currentState (car (remove smallPosition (list 0 1 2)))))
- (car (list-ref currentState (cadr (remove smallPosition (list 0 1 2))))))
- (helper d direction
- (list-update
- (list-update currentState (car (remove smallPosition (list 0 1 2))) cdr)
- (cadr (remove smallPosition (list 0 1 2)))
- (λ (x) (cons (car (list-ref currentState (car (remove smallPosition (list 0 1 2))))) x)))
- (add1 moveCount)
- smallPosition
- (cons (list (car (remove smallPosition (list 0 1 2))) (cadr (remove smallPosition (list 0 1 2)))) solution))]
- [else (helper d direction
- (list-update
- (list-update currentState (cadr (remove smallPosition (list 0 1 2))) cdr)
- (car (remove smallPosition (list 0 1 2)))
- (λ (x) (cons (car (list-ref currentState (cadr (remove smallPosition (list 0 1 2))))) x)))
- (add1 moveCount)
- smallPosition
- (cons (list (cadr (remove smallPosition (list 0 1 2))) (car (remove smallPosition (list 0 1 2)))) solution))]))
- (if (= (modulo discs 2) 0)
- (helper discs 1 (list (reverse (build-list (add1 discs) values)) (list 0) (list 0)) 0 0 empty)
- (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