 (ns clojure-club.meadow-finder.mgrimm
   (:require
     [clojure.set :as set]
-    [clojure.string :as string]))
-
-(defn ->cave [cave]
-  (reduce (fn [v x] (conj v (string/split x #""))) [] cave))
+    [clojure.string :as string]
+    [clojure-club.meadow-finder.core :as mc]))
 
 (defn neighbors [[y x]]
   (let [ys ((juxt dec identity inc identity) y)

       (drop-while last)
       ffirst))

(defn step-cave [cave [meadows seen [coord & coords]]]
  (let [cell (get-in cave coord)]
    (cond
      (seen coord) [meadows seen coords]
      (= cell " ") (let [b (meadow-from cave coord)] [(conj meadows b) (into seen b) coords])
      :else [meadows (conj seen coord) coords])))

(defn meadows [cave]
  (let [h (count cave), w (count (first cave))]
    (->> [[] #{} (for [y (range h), x (range w)] [y x])]
         (iterate (partial step-cave cave))
         (drop-while last)
         ffirst

;; -----------------------------------------------------------------------------

(comment
  (= [[[0 0]] [[1 1]]] (meadows mc/meadow-2x2-grid))
  (meadows mc/meadow-32x32x4))
