Skip to content

Instantly share code, notes, and snippets.

@tlewin
Forked from skuro/readme.org
Created February 13, 2019 20:12
Show Gist options
  • Save tlewin/f24af2532ef11956bc637b274b8458b3 to your computer and use it in GitHub Desktop.
Save tlewin/f24af2532ef11956bc637b274b8458b3 to your computer and use it in GitHub Desktop.
Meetup #111 solutions
(defn generate-row [n]
(map (fn [_]
(rand-int 2))
(range n)))
(defn generate-grid [n]
(map (fn [_] (generate-row n)) (range n)))
(generate-grid 5)
(defn one? [x]
(= x 1))
(defn count-ones
[coll]
(count (take-while one? coll)))
(defn- count-row
[row position]
(let [cell (nth row position)]
(if (zero? cell)
0
(let [[left right] (split-at position row)
[_ & right] right]
(+ (count-ones (reverse left))
(count-ones right))))))
(defn- grid-column
[grid column]
(map #(nth % column) grid))
(def my-grid
(generate-grid 5))
(defn calculate-grid [grid]
(let [size (count grid)
t (map #(grid-column grid %) (range size))]
(map (fn [row-number]
(map (fn [col-number]
(+ (count-row (nth grid row-number) col-number)
(count-row (nth t col-number) row-number)))
(range size)))
(range size))))
(defn print-grid
[grid]
(map #(println %) grid))
(print-grid my-grid)
(print-grid (calculate-grid my-grid))
(defn remove-random [grid]
(let [size (count grid)
budget (int (/ (* size size) 2))
flat-grid (map vector (flatten grid) (range (* size size)))
[keep drop] (split-at (inc budget) (shuffle flat-grid))
ndrop (map (fn [[v i]] [-1 i]) drop)]
(partition size (map first (sort-by last (concat keep ndrop))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment