Advertisement
Guest User

path-to-target

a guest
May 17th, 2012
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;;This assumes a 1-D vector of 36 elements to represent a 6x6 grid.
  2. ;;start-point must be one of the corners of the grid, that is, 0, 5, 30, or 35.
  3. ;;target can be any other number less than 36, and represents an index.
  4. (defn path-to-target [start-point target]
  5.   (loop [confirmed-squares []
  6.         current-square start-point]
  7.     (if (= current-square target)
  8.       confirmed-squares
  9.       (recur (conj confirmed-squares current-square)
  10.              (cond
  11.                (< 0 (- (rem current-square 6) (rem target 6))) (inc current-square)
  12.                (> 0 (- (rem current-square 6) (rem target 6))) (dec current-square)
  13.                :else (cond
  14.                    (< 0 (- (quot current-square 6) (quot target 6))) (+ 6 current-square)
  15.                    (> 0 (- (quot current-square 6) (quot target 6))) (- 6 current-square)
  16.                    :else target))
  17. ))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement