-
-
Save tonvanbart/7f968412e49022fc8851 to your computer and use it in GitHub Desktop.
This file contains 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 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