Skip to content

Commit

Permalink
Add q73.. this was rough
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkun committed Sep 18, 2024
1 parent 24cac26 commit 20e140d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/ex/q073.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
;; https://4clojure.oxal.org/#/problem/073

(ns ex.q073)

(defn ans
"Problem 73, Analyze a Tic-Tac-Toe Board"
[board]
(letfn [(win? [coll] (if (and (apply = coll) (not= (first coll) :e)) (first coll) nil))
(transpose [coll] (apply map vector coll))
(diag [coll i] (map (fn [[idx row]] (row idx)) (zipmap i coll)))]
(first (drop-while nil?
(lazy-cat
(map win? board)
(map win? (transpose board))
(map win? [(diag board [0 1 2]) (diag board [2 1 0])]))))))
29 changes: 29 additions & 0 deletions test/ex/q073_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
;; https://4clojure.oxal.org/#/problem/073

(ns ex.q073-test
(:require [clojure.test :as t]
[ex.q073 :as sut]))

(t/deftest test-q73
(t/testing "Problem 73, Analyze a Tic-Tac-Toe Board"
(t/is (= nil (sut/ans [[:e :e :e]
[:e :e :e]
[:e :e :e]])))
(t/is (= :x (sut/ans [[:x :e :o]
[:x :e :e]
[:x :e :o]])))
(t/is (= :o (sut/ans [[:e :x :e]
[:o :o :o]
[:x :e :x]])))
(t/is (= nil (sut/ans [[:x :e :o]
[:x :x :e]
[:o :x :o]])))
(t/is (= :x (sut/ans [[:x :e :e]
[:o :x :e]
[:o :e :x]])))
(t/is (= :o (sut/ans [[:x :e :o]
[:x :o :e]
[:o :e :x]])))
(t/is (= nil (sut/ans [[:x :o :x]
[:x :o :x]
[:o :x :o]])))))

0 comments on commit 20e140d

Please sign in to comment.