Skip to content

Instantly share code, notes, and snippets.

@tonvanbart
Forked from skuro/gist:3286e7a53d4938437ed1
Last active August 29, 2015 14:18
Show Gist options
  • Save tonvanbart/7f968412e49022fc8851 to your computer and use it in GitHub Desktop.
Save tonvanbart/7f968412e49022fc8851 to your computer and use it in GitHub Desktop.
(ns minesweeper.solver
(:use clojure.test))
(defn solve [scenario]
"Example input: {:rows 10 :cols 10 :mines 2}"
(let [mines (:mines scenario)
rows (:rows scenario)
cols (:cols scenario)
freecells (- (* rows cols) mines)]
(cond
(= 0 mines) true
(= cols rows 1) false
(and (= mines 1) (> rows 1) (= cols 1)) true
(and (= mines 1) (= rows 1) (> cols 1)) true
(and (= rows cols 2)) false
(and (or (= rows 2) (= cols 2)) (even? freecells)) true
:else false)))
(deftest test-solve
(are [challenge solution] (= solution (solve challenge))
{:rows 1 :cols 1 :mines 1} false
{:rows 1 :cols 1 :mines 0} true
{:rows 2 :cols 1 :mines 1} true
{:rows 2 :cols 2 :mines 1} false
{:rows 2 :cols 1000 :mines 10} true
{:rows 2 :cols 1000 :mines 11} false
))
(run-tests)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment