Skip to content

Instantly share code, notes, and snippets.

@jaydeesimon
Created March 10, 2017 21:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaydeesimon/c40cd6e768126e391e3b57f89dcc82cf to your computer and use it in GitHub Desktop.
Save jaydeesimon/c40cd6e768126e391e3b57f89dcc82cf to your computer and use it in GitHub Desktop.
(defn neighbors [board coord]
(let [directions [[1 0] [-1 0] [0 1] [0 -1]]]
(->> (map #(mapv + coord %) directions)
(filter (fn [coord']
(= (get-in board coord') \.))))))
(defn bfs-path [board start end]
(loop [frontier [[start [start]]]
visited #{start}]
(let [[[current current-path] & frontier] frontier
unvisited-neighbors (->> current
(neighbors board)
(remove visited))]
(cond (nil? current) []
(= current end) current-path
:else (recur (into (vec frontier)
(mapv #(vector % (conj current-path %)) unvisited-neighbors))
(into visited [current]))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment