Skip to content

Instantly share code, notes, and snippets.

@candera
Created October 13, 2011 15:15
Show Gist options
  • Save candera/1284476 to your computer and use it in GitHub Desktop.
Save candera/1284476 to your computer and use it in GitHub Desktop.
Solve a 3x3 grid puzzle: all rows must add to 15
(use 'clojure.math.combinatorics)
(def possible-answers (permutations (range 1 10)))
(defn sum-els [coll a b c]
(+ (nth coll a)
(nth coll b)
(nth coll c)))
(defn right-answer [coll]
(= 15
(sum-els coll 0 1 2)
(sum-els coll 3 4 5)
(sum-els coll 6 7 8)
(sum-els coll 0 3 6)
(sum-els coll 1 4 7)
(sum-els coll 2 5 8)
(sum-els coll 0 4 8)
(sum-els coll 2 4 6)))
(def answers (filter right-answer possible-answers))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment