 (defn tile-at-coord? [board tile coord]
   (= (tile->coords board tile) coord))
 
+(defn ranked-moves [board dist-map tile avoid]
+  (let [moves (clojure.set/difference (set (adjacent-tiles board tile)) avoid)]
+    (sort-by #(nth dist-map (tile->index board %)) moves)))
+
 (defn path-to [board goal & [avoid]]
   (let [dist-map (distance-map board goal avoid)]
     (if (impassable? board dist-map 0)

      (loop [path '() board board]
        (if (tile-at-coord? board tile goal)
          path
          (let [best-move (first (sort-by #(nth dist-map (tile->index board %))
                                          (clojure.set/difference
                                           (set (adjacent-tiles board tile))
                                           avoid)))
                moves (move-tile board tile (tile->coords board best-move) avoid)]
            (if (empty? moves)
              '()

      (loop [path '() board board]
        (if (tile-at-coord? board 0 goal)
          (reverse path)
          (let [best-move (first (ranked-moves board dist-map 0 avoid))]
            (if (nil? best-move) '()
                (recur (cons best-move path)
                       (slide board best-move)))))))))

(defn move-tile [board tile goal & [avoid]]
  (if (tile-at-coord? board tile goal)
