Skip to content

Instantly share code, notes, and snippets.

Created September 25, 2011 21:14
Show Gist options
  • Save anonymous/1241164 to your computer and use it in GitHub Desktop.
Save anonymous/1241164 to your computer and use it in GitHub Desktop.
;; amalloy's solution to For Science!
;; https://4clojure.com/problem/117
(fn [board]
(let [names {\space :open
\# :wall
\M :goal
\C :start}
board (vec (for [row board]
(vec (for [col row]
(names col)))))
h (count board)
w (count (board 0))
[start goal] (for [token [:start :goal]
y (range h)
:let [row (board y)]
x (range w)
:when (= token (row x))]
[y x])
valid? (fn [[y x :as pos]]
(and (not-any? neg? pos)
(< y h) (< x w)))
deltas [[0 1] [1 0] [0 -1] [-1 0]]
neighbors (fn [pos]
(filter valid?
(for [d deltas]
(map + pos d))))
reachable? (fn reachable? [board pos]
(and (not= :wall (get-in board pos))
(or (= goal pos)
(let [new-board (assoc-in board pos :wall)]
(some #(reachable? new-board %) (neighbors pos))))))]
(boolean (reachable? board start))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment