GitHub PullRequestReviewCommentEvent at 2014-02-26 01:19:16
Repository: maize
Actor: r00k
Payload: {"comment":{"url":"https://api.github.com/repos/clojure-rica/maize/pulls/comments/10064722","id":10064722,"diff_hunk":"@@ -21,63 +21,67 @@\n (defn- blocked-by-wall? [current-location walls neighbor]\n   (walls #{current-location neighbor}))\n \n-(defn reachable-neighbors [location visited walls size]\n-  (let [within-maze-and-unvisited (unvisited-neighbors location visited size)]\n+(defn reachable-neighbors [location {:keys [visited walls size]\n+                                     :or {walls {}}}]\n+  (let [within-maze-and-unvisited (unvisited-neighbors location {:visited visited\n+                                                                 :size size})]\n     (set\n       (remove (partial blocked-by-wall? location walls)\n               within-maze-and-unvisited))))\n \n-(defn- random-visitable-neighbor [location visited size]\n-  (rand-nth (seq (unvisited-neighbors location visited size))))\n-\n-(defn- walls [grid doors]\n-  (difference grid doors))\n+(defn- walls-without-doors [walls doors]\n+  (difference walls doors))\n \n (defn- all-locations [size]\n   (for [x (range size) y (range size)] [x y]))\n \n-(defn- all-walls [size location]\n-  (map (partial conj #{} location) (unvisited-neighbors location #{} size)))\n+(defn- all-walls-for-location [size location]\n+  (map\n+    (partial conj #{} location)\n+    (unvisited-neighbors location {:visited #{}\n+                                   :size size})))\n+\n+(defn- all-walls [size]\n+  (reduce into #{} (map (partial all-walls-for-location size)\n+                        (all-locations size))))\n+\n+(defn- all-walls-without-doors [size doors]\n+  (walls-without-doors (all-walls size) doors))\n \n-(defn- fully-walled-grid [size]\n-  (reduce into #{} (map (partial all-walls size) (all-locations size))))\n+(defn- visited? [location {:keys [visited size]}]\n+  (= (count visited) (* size size)))\n \n-(defn generate-maze [{:keys [path visited doors size next-location-fn]\n-                      :or {next-location-fn random-visitable-neighbor}}]\n-  (if-let [current-location (peek path)]\n-    (if-let [next-location (next-location-fn current-location visited size)]\n-      (recur {:path (conj path next-location)\n-              :visited (conj visited current-location)\n-              :doors (conj doors #{current-location next-location})\n-              :size size\n-              :next-location-fn next-location-fn})\n-      (recur {:path (pop path)\n-              :visited (conj visited current-location)\n-              :doors doors\n-              :size size\n-              :next-location-fn next-location-fn}))\n-    (walls (fully-walled-grid size) doors)))\n+(defn- solved? [location {:keys [size]}]\n+  (= location [(dec size) (dec size)]))\n \n-(defn solve-maze [{:keys [path visited walls size update-channel]\n-                   :or {update-channel nil}}]\n+(defn- random-reachable-neighbor [location {:keys [visited walls size]\n+                                            :or {walls {}}}]\n+  (rand-nth (seq (reachable-neighbors location {:visited visited\n+                                                :walls walls\n+                                                :size size}))))\n+\n+(defn search-maze [{:keys [path visited walls doors size update-channel next-location-fn finished-fn]\n+                    :or {next-location-fn random-reachable-neighbor\n+                         path [[0 0]]\n+                         visited #{}\n+                         doors #{}}\n+                    :as maze}]\n   (let [current-location (peek path)]\n-    (when update-channel\n-      (go (>! update-channel {:walls walls :path path :visited visited})))\n-    (if (= current-location [(dec size) (dec size)])\n-      (do\n-        (when update-channel\n-          (go (>! update-channel :solved)))\n-        path)\n-      (if-let [next-location (rand-nth\n-                               (seq\n-                                 (reachable-neighbors current-location visited walls size)))]\n-        (solve-maze {:path (conj path next-location)\n-                     :visited (conj visited current-location)\n-                     :walls walls\n-                     :size size\n-                     :update-channel update-channel})\n-        (solve-maze {:path (pop path)\n-                     :visited (conj visited current-location)\n-                     :walls walls\n-                     :size size\n-                     :update-channel update-channel})))))\n+    (do\n+      (when update-channel (go (>! update-channel maze)))\n+      (if (finished-fn current-location maze)\n+        (do\n+          (when update-channel (go (>! update-channel :finished)))\n+          (merge\n+            maze {:walls (all-walls-without-doors size doors)}))\n+        (let [maze (merge maze {:visited (conj visited current-location)})]\n+          (if-let [next-location (next-location-fn current-location maze)]\n+            (search-maze (merge maze {:path (conj path next-location)\n+                                      :doors (conj doors #{current-location next-location})}))\n+            (search-maze (merge maze {:path (pop path)}))))))))\n+\n+(defn generate-maze [maze]","path":"src-cljs/maze/core.cljs","position":120,"original_position":120,"commit_id":"52f6ebef0ca86fa45f1336dee7cf6922d73c4458","original_commit_id":"52f6ebef0ca86fa45f1336dee7cf6922d73c4458","user":{"login":"r00k","id":46677,"avatar_url":"https://gravatar.com/avatar/e30754b725a8b734f1a507beac3063c6?d=https%3A%2F%2Fidenticons.github.com%2Fe24a9dd946214b62855f5056c64b25c7.png&r=x","gravatar_id":"e30754b725a8b734f1a507beac3063c6","url":"https://api.github.com/users/r00k","html_url":"https://github.com/r00k","followers_url":"https://api.github.com/users/r00k/followers","following_url":"https://api.github.com/users/r00k/following{/other_user}","gists_url":"https://api.github.com/users/r00k/gists{/gist_id}","starred_url":"https://api.github.com/users/r00k/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/r00k/subscriptions","organizations_url":"https://api.github.com/users/r00k/orgs","repos_url":"https://api.github.com/users/r00k/repos","events_url":"https://api.github.com/users/r00k/events{/privacy}","received_events_url":"https://api.github.com/users/r00k/received_events","type":"User","site_admin":false},"body":"Huge win on this fn and the next. :+1: ","created_at":"2014-02-26T01:19:16Z","updated_at":"2014-02-26T01:19:16Z","html_url":"https://github.com/clojure-rica/maize/pull/3#discussion_r10064722","pull_request_url":"https://api.github.com/repos/clojure-rica/maize/pulls/3","_links":{"self":{"href":"https://api.github.com/repos/clojure-rica/maize/pulls/comments/10064722"},"html":{"href":"https://github.com/clojure-rica/maize/pull/3#discussion_r10064722"},"pull_request":{"href":"https://api.github.com/repos/clojure-rica/maize/pulls/3"}}}}