Skip to content

Instantly share code, notes, and snippets.

@thattommyhall
Created June 17, 2012 15:39
Show Gist options
  • Select an option

  • Save thattommyhall/2944874 to your computer and use it in GitHub Desktop.

Select an option

Save thattommyhall/2944874 to your computer and use it in GitHub Desktop.
Soln to 119
(fn [player board]
(let [column
(fn [[x y]]
(for [newy [0 1 2]]
[x newy]))
row
(fn [[x y]]
(for [newx [0 1 2]]
[newx y]))
diags
(fn [[x y :as pos]]
(let [up [[0 2] [1 1] [2 0]]
down [[0 0] [1 1] [2 2]]]
(filter #(some (partial = pos) %) [up down])))
triples
(fn [pos]
(into (diags pos) [(row pos) (column pos)]))
winning?
(fn [board triple]
(every? #(= player (get-in board %)) triple))
empties
(fn [board]
(for [x [0 1 2]
y [0 1 2]
:when (= :e (get-in board [x y]))]
[x y]))
]
(apply hash-set
(filter
(fn [pos]
(let [new-board (assoc-in board pos player)
lines (triples pos)]
(some #(winning? new-board %) lines)))
(empties board)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment