-
-
Save pieter-van-prooijen/b639f0ae2846bebcc1eafd2165ce81f6 to your computer and use it in GitHub Desktop.
Meetup #111 solutions
This file contains hidden or 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 clojure-dojo-2019-02-13.core) | |
(defn board [width height] | |
(into {} (for [x (range width) | |
y (range height)] | |
[[x y] (rand-nth [:blue :red])]))) | |
(defn neighbours [board width height x y step-x step-y] | |
(let [end-x (if (neg? step-x) -1 width) | |
end-y (if (neg? step-y) -1 height)] | |
(count (concat (take-while (fn [x1] | |
(= (get board [x1 y]) :blue)) | |
(range (+ x step-x) end-x step-x)) | |
(take-while (fn [y1] | |
(= (get board [x y1]) :blue)) | |
(range (+ y step-y) end-y step-y)))))) | |
(defn all-neighbours [board width height x y] | |
(+ (neighbours board width height x y -1 -1) | |
(neighbours board width height x y 1 1))) | |
(defn compute-numbers [board width height] | |
(reduce (fn [board1 [x y]] | |
(if (= (get board1 [x y]) :blue) | |
(assoc board1 [x y] (all-neighbours board width height x y)) | |
board1)) | |
board | |
(keys board))) | |
(defn remove-dots [board] | |
(reduce (fn [board1 [x y]] | |
(if (= :remove (rand-nth [:keep :remove])) | |
(assoc board1 [x y] :grey) | |
(board1))) | |
board | |
(keys board))) | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment