Created
March 23, 2017 19:06
-
-
Save egregius313/c8c33ac89a225f2a052a035b96089586 to your computer and use it in GitHub Desktop.
Basic Clojure implementation of the Blob problem for solving mazes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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