Skip to content

Instantly share code, notes, and snippets.

@egregius313
Created March 23, 2017 19:06
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 egregius313/c8c33ac89a225f2a052a035b96089586 to your computer and use it in GitHub Desktop.
Save egregius313/c8c33ac89a225f2a052a035b96089586 to your computer and use it in GitHub Desktop.
Basic Clojure implementation of the Blob problem for solving mazes
(ns blob)
(defn abnormal? [color]
(= color :abnormal))
(defn color [grid x y]
(get-in grid [x y]))
(defn recolor [grid x y color]
(assoc-in grid [x y] color))
(defn column-count [grid]
(count (first grid)))
(defn row-count [grid]
(count grid))
(defn neighbors [grid x y]
[[x (dec y)] [x (inc y)]
[(dec x) y] [(inc x) y]])
(defn collect-cells [grid x y visited]
(if-not (abnormal? (color grid x y))
visited
(if (contains? visited [x y])
visited
(reduce (fn [vis [i j]]
(collect-cells grid i j vis))
(conj visited [x y])
(neighbors grid x y)))))
(defn count-cells [grid x y]
(count (collect-cells grid x y #{})))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment