This repository has been archived by the owner on May 30, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
83a3b17
commit f74acae
Showing
8 changed files
with
197 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Instructions | ||
|
||
Count the scored points on a Go board. | ||
|
||
In the game of go (also known as baduk, igo, cờ vây and wéiqí) points are gained by completely encircling empty intersections with your stones. | ||
The encircled intersections of a player are known as its territory. | ||
|
||
Write a function that determines the territory of each player. | ||
You may assume that any stones that have been stranded in enemy territory have already been taken off the board. | ||
|
||
Write a function that determines the territory which includes a specified coordinate. | ||
|
||
Multiple empty intersections may be encircled at once and for encircling only horizontal and vertical neighbors count. | ||
In the following diagram the stones which matter are marked "O" and the stones that don't are marked "I" (ignored). | ||
Empty spaces represent empty intersections. | ||
|
||
```text | ||
+----+ | ||
|IOOI| | ||
|O O| | ||
|O OI| | ||
|IOI | | ||
+----+ | ||
``` | ||
|
||
To be more precise an empty intersection is part of a player's territory if all of its neighbors are either stones of that player or empty intersections that are part of that player's territory. | ||
|
||
For more information see [wikipedia][go-wikipedia] or [Sensei's Library][go-sensei]. | ||
|
||
[go-wikipedia]: https://en.wikipedia.org/wiki/Go_%28game%29 | ||
[go-sensei]: https://senseis.xmp.net/ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"authors": [ | ||
"ErikSchierboom" | ||
], | ||
"files": { | ||
"solution": [ | ||
"src/go_counting.cljs" | ||
], | ||
"test": [ | ||
"test/go_counting_test.cljs" | ||
], | ||
"example": [ | ||
".meta/src/example.cljs" | ||
] | ||
}, | ||
"blurb": "Count the scored points on a Go board." | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
(ns go-counting) | ||
|
||
(defn grid->graph [xs] | ||
(->> (for [x (-> xs count range), y (-> xs first count range)] | ||
(case (get-in xs [x y]) | ||
\W [[y x] :white] | ||
\B [[y x] :black] | ||
\space [[y x] :free])) | ||
(into {}))) | ||
|
||
(defn neighbors [pred stones [x y]] | ||
(->> [[0 1] [0 -1] [1 0] [-1 0]] | ||
(map (fn [[j k]] [(+ x j) (+ y k)])) | ||
(filter (comp pred stones)))) | ||
|
||
(defn territory-of [stones [x y]] | ||
(if (= :free (stones [x y])) | ||
(letfn [(f [[seen frontier]] | ||
(let [nseen (reduce conj seen frontier)] | ||
[nseen (->> frontier | ||
(mapcat #(neighbors #{:free} stones %)) | ||
(filter (complement nseen)))]))] | ||
(->> [#{} [[x y]]] | ||
(iterate f) | ||
(drop-while (comp seq second)) | ||
(ffirst))) | ||
#{})) | ||
|
||
(defn territory-owner [stones territory] | ||
(->> territory | ||
(mapcat (partial neighbors #{:black :white} stones)) | ||
(map stones) | ||
(#(cond (empty? %) nil | ||
(every? (partial = :black) %) :black | ||
(every? (partial = :white) %) :white | ||
:else nil)))) | ||
|
||
(defn territory [grid [x y]] | ||
(let [stones (grid->graph grid) | ||
territory (territory-of stones [x y])] | ||
(if (nil? (stones [x y])) | ||
(throw (new js/Error "Invalid coordinate!")) | ||
{:stones territory :owner (territory-owner stones territory)}))) | ||
|
||
(defn territories [grid] | ||
(let [territories (->> grid grid->graph keys (map (partial territory grid))) | ||
territory-for #(->> territories (filter (comp % :owner)) (map :stones) (reduce concat) set)] | ||
{:black-territory (territory-for (partial = :black)) | ||
:white-territory (territory-for (partial = :white)) | ||
:null-territory (territory-for nil?)})) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# This is an auto-generated file. Regular comments will be removed when this | ||
# file is regenerated. Regenerating will not touch any manually added keys, | ||
# so comments can be added in a "comment" key. | ||
|
||
[94d0c01a-17d0-424c-aab5-2736d0da3939] | ||
description = "Black corner territory on 5x5 board" | ||
|
||
[b33bec54-356a-485c-9c71-1142a9403213] | ||
description = "White center territory on 5x5 board" | ||
|
||
[def7d124-422e-44ae-90e5-ceda09399bda] | ||
description = "Open corner territory on 5x5 board" | ||
|
||
[57d79036-2618-47f4-aa87-56c06d362e3d] | ||
description = "A stone and not a territory on 5x5 board" | ||
|
||
[0c84f852-e032-4762-9010-99f6a001da96] | ||
description = "Invalid because X is too low for 5x5 board" | ||
|
||
[6f867945-9b2c-4bdd-b23e-b55fe2069a68] | ||
description = "Invalid because X is too high for 5x5 board" | ||
|
||
[d67aaffd-fdf1-4e7f-b9e9-79897402b64a] | ||
description = "Invalid because Y is too low for 5x5 board" | ||
|
||
[14f23c25-799e-4371-b3e5-777a2c30357a] | ||
description = "Invalid because Y is too high for 5x5 board" | ||
|
||
[37fb04b5-98c1-4b96-8c16-af2d13624afd] | ||
description = "One territory is the whole board" | ||
|
||
[9a1c59b7-234b-495a-8d60-638489f0fc0a] | ||
description = "Two territory rectangular board" | ||
|
||
[d1645953-1cd5-4221-af6f-8164f96249e1] | ||
description = "Two region rectangular board" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{:deps | ||
{org.clojure/clojure {:mvn/version "1.10.1"} | ||
org.clojure/clojurescript {:mvn/version "1.10.773"}} | ||
|
||
:aliases | ||
{:test | ||
{:extra-paths ["test"] | ||
:extra-deps | ||
{olical/cljs-test-runner {:mvn/version "3.8.0"}} | ||
:main-opts ["-m" "cljs-test-runner.main"]}}} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
(ns go-counting) | ||
|
||
(defn territory [grid [x y]]) | ||
|
||
(defn territories [grid]) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
(ns go-counting-test | ||
(:require [clojure.test :refer [deftest is]] | ||
[go-counting :as g])) | ||
|
||
(def example | ||
[" B " | ||
" B B " | ||
"B W B" | ||
" W W " | ||
" W "]) | ||
|
||
(deftest territory | ||
(is (= (g/territory example [0 1]) | ||
{:stones #{[0 0] [0 1] [1 0]} :owner :black})) | ||
(is (= (g/territory example [2 3]) | ||
{:stones #{[2 3]} :owner :white})) | ||
(is (= (g/territory example [1 4]) | ||
{:stones #{[0 3] [0 4] [1 4]} :owner nil})) | ||
(is (= (g/territory example [1 1]) | ||
{:stones #{} :owner nil})) | ||
(is (thrown? js/Error (g/territory example [-1 1]))) | ||
(is (thrown? js/Error (g/territory example [5 1]))) | ||
(is (thrown? js/Error (g/territory example [1 -1]))) | ||
(is (thrown? js/Error (g/territory example [1 5])))) | ||
|
||
(deftest territories | ||
(is (= (g/territories [" "]) | ||
{:black-territory #{} | ||
:white-territory #{} | ||
:null-territory #{[0 0]}})) | ||
(is (= (g/territories [" BW " " BW "]) | ||
{:black-territory #{[0 0] [0 1]} | ||
:white-territory #{[3 0] [3 1]} | ||
:null-territory #{}})) | ||
(is (= (g/territories [" B "]) | ||
{:black-territory #{[0 0] [2 0]} | ||
:white-territory #{} | ||
:null-territory #{}}))) |