Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#768] New practice exercise go-counting #776

Merged
merged 5 commits into from
Jun 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2446,6 +2446,31 @@
],
"difficulty": 4
},
{
"slug": "go-counting",
"name": "Go Counting",
"uuid": "560347a2-9f20-4bad-b932-72a75ee08e14",
"prerequisites": [
"atoms",
"if",
"case",
"cond",
"strings",
"enum",
"default-arguments",
"guards",
"integers",
"list-comprehensions",
"maps",
"pattern-matching",
"pipe-operator",
"recursion"
],
"practices": [
"list-comprehensions"
],
"difficulty": 9
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It took me a while to solve, so I put a high difficulty, but maybe I overdid it?

Anything go-related probably deserves a 9 😬

},
{
"slug": "yacht",
"name": "Yacht",
Expand Down
36 changes: 36 additions & 0 deletions exercises/practice/go-counting/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Description

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 neighbours 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 neighbours are either stones of that player or empty
intersections that are part of that player's territory.

For more information see
[wikipedia](https://en.wikipedia.org/wiki/Go_%28game%29) or [Sensei's
Library](http://senseis.xmp.net/).
4 changes: 4 additions & 0 deletions exercises/practice/go-counting/.formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
18 changes: 18 additions & 0 deletions exercises/practice/go-counting/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"authors": ["jiegillet"],
"contributors": [
"angelikatyborska"
],
"files": {
"example": [
".meta/example.ex"
],
"solution": [
"lib/go_counting.ex"
],
"test": [
"test/go_counting_test.exs"
]
},
"blurb": "Count the scored points on a Go board."
}
128 changes: 128 additions & 0 deletions exercises/practice/go-counting/.meta/example.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
defmodule GoCounting do
@type position :: {integer, integer}
@type owner :: %{owner: atom, territory: [position]}
@type territories :: %{white: [position], black: [position], none: [position]}

@doc """
Return the owner and territory around a position

"""
@spec territory(board :: String.t(), position :: position) ::
{:ok, owner} | {:error, String.t()}
def territory(board, {x, y} = pos) do
size_x = String.split(board, "\n") |> hd |> String.length()
size_y = String.split(board, "\n", trim: true) |> length()

if x < 0 or x >= size_x or y < 0 or y >= size_y do
{:error, "Invalid coordinate"}
else
owner =
board
|> make_graph
|> expand_territory([pos])
|> get_owner

{:ok, owner}
end
end

@doc """
Return all white, black and neutral territories
"""
@spec territories(board :: String.t()) :: territories
def territories(board) do
graph = make_graph(board)

empties = for {pos, {:none, _neighbors}} <- graph, do: pos

territories(graph, empties, %{white: [], black: [], none: []})
end

def territories(_graph, [], territories), do: territories

def territories(graph, [pos | positions], territories) do
%{owner: owner, territory: territory} =
graph
|> expand_territory([pos])
|> get_owner

positions = Enum.reject(positions, &(&1 in territory))
territories = %{territories | owner => Enum.sort(territories[owner] ++ territory)}

territories(graph, positions, territories)
end

def to_color(?W), do: :white
def to_color(?B), do: :black
def to_color(?_), do: :none

def make_graph(board) do
board =
board
|> String.split("\n", trim: true)
|> Enum.map(fn row -> row |> to_charlist |> Enum.map(&to_color/1) end)

left_right_edges =
for {[color], r} <- Enum.with_index(board) do
# For rows with a single column we cannot use zip
%{{0, r} => {color, []}}
end ++
for {row, r} <- Enum.with_index(board),
{{cell, right_cell}, c} <- Enum.zip(row, tl(row)) |> Enum.with_index() do
# For rows with multiple columns, we zip
%{{c, r} => {cell, [{c + 1, r}]}, {c + 1, r} => {right_cell, [{c, r}]}}
end

top_down_edges =
case board do
[row] ->
for {color, c} <- Enum.with_index(row), do: %{{c, 0} => {color, []}}

_ ->
for {{row, row_below}, r} <- Enum.zip(board, tl(board)) |> Enum.with_index(),
{{cell, below_cell}, c} <- Enum.zip(row, row_below) |> Enum.with_index() do
%{{c, r} => {cell, [{c, r + 1}]}, {c, r + 1} => {below_cell, [{c, r}]}}
end
end

Enum.reduce(
left_right_edges ++ top_down_edges,
%{},
&Map.merge(&1, &2, fn _key, {cell, n1}, {cell, n2} -> {cell, n1 ++ n2} end)
)
end

def expand_territory(graph, positions, visited \\ MapSet.new())
def expand_territory(_graph, [], _visited), do: []

def expand_territory(graph, [pos | positions], visited) do
{color, neighbors} =
case graph[pos] do
{:white, _neighbors} ->
{:white, []}

{:black, _neighbors} ->
{:black, []}

{:none, neighbors} ->
{:none, Enum.reject(neighbors, &(&1 in visited))}
end

[{pos, color} | expand_territory(graph, neighbors ++ positions, MapSet.put(visited, pos))]
end

def get_owner(territory) do
empties = for {pos, :none} <- territory, do: pos
colors = for {_pos, color} when color != :none <- territory, do: color

%{
territory: Enum.sort(empties),
owner:
case {Enum.empty?(empties), Enum.uniq(colors)} do
{false, [:white]} -> :white
{false, [:black]} -> :black
_ -> :none
end
}
end
end
42 changes: 42 additions & 0 deletions exercises/practice/go-counting/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via 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"
20 changes: 20 additions & 0 deletions exercises/practice/go-counting/lib/go_counting.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
defmodule GoCounting do
@type position :: {integer, integer}
@type owner :: %{owner: atom, territory: [position]}
@type territories :: %{white: [position], black: [position], none: [position]}
Comment on lines +2 to +4
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


@doc """
Return the owner and territory around a position
"""
@spec territory(board :: String.t(), position :: position) ::
{:ok, owner} | {:error, String.t()}
def territory(board, {x, y} = pos) do
end

@doc """
Return all white, black and neutral territories
"""
@spec territories(board :: String.t()) :: territories
def territories(board) do
end
end
28 changes: 28 additions & 0 deletions exercises/practice/go-counting/mix.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
defmodule GoCounting.MixProject do
use Mix.Project

def project do
[
app: :go_counting,
version: "0.1.0",
# elixir: "~> 1.8",
start_permanent: Mix.env() == :prod,
deps: deps()
]
end

# Run "mix help compile.app" to learn about applications.
def application do
[
extra_applications: [:logger]
]
end

# Run "mix help deps" to learn about dependencies.
defp deps do
[
# {:dep_from_hexpm, "~> 0.3.0"},
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
]
end
end
Loading