Skip to content

Instantly share code, notes, and snippets.

Created March 2, 2012 07:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/1956373 to your computer and use it in GitHub Desktop.
Save anonymous/1956373 to your computer and use it in GitHub Desktop.
;; ariarule's solution to Analyze a Tic-Tac-Toe Board
;; https://4clojure.com/problem/73
(fn analyze-tic-tac-toe [board]
(let [things-to-check
[{:step (fn [z] [(first z),(inc (second z))])
:start [[0,0] [1,0] [2,0]]}
{:step (fn [z] [(inc (first z)),(second z)])
:start [[0,0] [0,1] [0,2]]}
{:step (fn [z] [(inc (first z)),(inc (second z))])
:start [[0,0]]}
{:step (fn [z] [(inc (first z)),(dec (second z))])
:start [[0,2]]}]
pieces #{:x :o}]
(some pieces (map
#(if (reduce
(fn [b3 thing-to-check]
(or b3 (reduce
(fn [b2 start]
(or b2 (reduce
(fn [b coord] (let [[x y] coord]
(and b (= % (nth (nth board x) y)))))
true
(take 3 (iterate (thing-to-check :step) start)))))
false
(thing-to-check :start))))
false
things-to-check)
%)
pieces))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment