Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. (ns blob)
  2.  
  3.  
  4. (defn abnormal? [color]
  5. (= color :abnormal))
  6.  
  7.  
  8. (defn color [grid x y]
  9. (get-in grid [x y]))
  10.  
  11.  
  12. (defn recolor [grid x y color]
  13. (assoc-in grid [x y] color))
  14.  
  15.  
  16. (defn column-count [grid]
  17. (count (first grid)))
  18.  
  19.  
  20. (defn row-count [grid]
  21. (count grid))
  22.  
  23.  
  24. (defn neighbors [grid x y]
  25. [[x (dec y)] [x (inc y)]
  26. [(dec x) y] [(inc x) y]])
  27.  
  28.  
  29. (defn collect-cells [grid x y visited]
  30. (if-not (abnormal? (color grid x y))
  31. visited
  32. (if (contains? visited [x y])
  33. visited
  34. (reduce (fn [vis [i j]]
  35. (collect-cells grid i j vis))
  36. (conj visited [x y])
  37. (neighbors grid x y)))))
  38.  
  39.  
  40. (defn count-cells [grid x y]
  41. (count (collect-cells grid x y #{})))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement