Skip to content

Commit

Permalink
Add connect exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSchierboom committed Dec 30, 2024
1 parent 783429f commit 52dcd8e
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,14 @@
"prerequisites": [],
"difficulty": 7
},
{
"slug": "connect",
"name": "Connect",
"uuid": "0c4ca569-6619-4515-9ee5-592224ed553b",
"practices": [],
"prerequisites": [],
"difficulty": 7
},
{
"slug": "binary-search",
"name": "Binary Search",
Expand Down
27 changes: 27 additions & 0 deletions exercises/practice/connect/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Instructions

Compute the result for a game of Hex / Polygon.

The abstract boardgame known as [Hex][hex] / Polygon / CON-TAC-TIX is quite simple in rules, though complex in practice.
Two players place stones on a parallelogram with hexagonal fields.
The player to connect his/her stones to the opposite side first wins.
The four sides of the parallelogram are divided between the two players (i.e. one player gets assigned a side and the side directly opposite it and the other player gets assigned the two other sides).

Your goal is to build a program that given a simple representation of a board computes the winner (or lack thereof).
Note that all games need not be "fair".
(For example, players may have mismatched piece counts or the game's board might have a different width and height.)

The boards look like this:

```text
. O . X .
. X X O .
O O O X .
. X O X O
X O O O X
```

"Player `O`" plays from top to bottom, "Player `X`" plays from left to right.
In the above example `O` has made a connection from left to right but nobody has won since `O` didn't connect top and bottom.

[hex]: https://en.wikipedia.org/wiki/Hex_%28board_game%29
17 changes: 17 additions & 0 deletions exercises/practice/connect/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"authors": [
"erikschierboom"
],
"files": {
"solution": [
"connect.ua"
],
"test": [
"tests.ua"
],
"example": [
".meta/example.ua"
]
},
"blurb": "Compute the result for a game of Hex / Polygon."
}
9 changes: 9 additions & 0 deletions exercises/practice/connect/.meta/example.ua
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Parse ← ⊜∘≠@\n. ▽≠@ .
Neighbors ← +[¯1_0 ¯1_1 0_¯1 0_1 1_¯1 1_0]¤
Step ← ▽⊸∊⊃(⋅Neighbors|⊚=⊙⋅)
Start! ← ▽≡(=0^0).⊚=
End! ← =∩^0⊙(-1△)
Wins‼! ← ±⧻♭≡(path(^1|^2)) ⊙¤ ⊸^0
XWins ← Wins‼!(Start!⊣ @X|Step @X|End!⊣)
OWins ← Wins‼!(Start!⊢ @O|Step @O|End!⊢)
Winner ← ⨬("None"|"Black"|"White") +⊃(XWins|×2OWins) Parse
40 changes: 40 additions & 0 deletions exercises/practice/connect/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# 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.

[6eff0df4-3e92-478d-9b54-d3e8b354db56]
description = "an empty board has no winner"

[298b94c0-b46d-45d8-b34b-0fa2ea71f0a4]
description = "X can win on a 1x1 board"

[763bbae0-cb8f-4f28-bc21-5be16a5722dc]
description = "O can win on a 1x1 board"

[819fde60-9ae2-485e-a024-cbb8ea68751b]
description = "only edges does not make a winner"

[2c56a0d5-9528-41e5-b92b-499dfe08506c]
description = "illegal diagonal does not make a winner"

[41cce3ef-43ca-4963-970a-c05d39aa1cc1]
description = "nobody wins crossing adjacent angles"

[cd61c143-92f6-4a8d-84d9-cb2b359e226b]
description = "X wins crossing from left to right"

[73d1eda6-16ab-4460-9904-b5f5dd401d0b]
description = "O wins crossing from top to bottom"

[c3a2a550-944a-4637-8b3f-1e1bf1340a3d]
description = "X wins using a convoluted path"

[17e76fa8-f731-4db7-92ad-ed2a285d31f3]
description = "X wins using a spiral path"
3 changes: 3 additions & 0 deletions exercises/practice/connect/connect.ua
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Compute the result for a game of Hex/Polygon
# Winner ? Board
Winner ← |1 ⊙(⍤"Please implement Winner" 0)
76 changes: 76 additions & 0 deletions exercises/practice/connect/tests.ua
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
~ "connect.ua" ~ Winner

# An empty board has no winner
Board ← $ . . . . .
$ . . . . .
$ . . . . .
$ . . . . .
$ . . . . .
⍤⤙≍ "None" Winner Board

# X can win on a 1x1 board
Board ← $ X
⍤⤙≍ "Black" Winner Board

# O can win on a 1x1 board
Board ← $ O
⍤⤙≍ "White" Winner Board

# Only edges does not make a winner
Board ← $ O O O X
$ X . . X
$ X . . X
$ X O O O
⍤⤙≍ "None" Winner Board

# Illegal diagonal does not make a winner
Board ← $ X O . .
$ O X X X
$ O X O .
$ . O X .
$ X X O O
⍤⤙≍ "None" Winner Board

# Nobody wins crossing adjacent angles
Board ← $ X . . .
$ . X O .
$ O . X O
$ . O . X
$ . . O .
⍤⤙≍ "None" Winner Board

# X wins crossing from left to right
Board ← $ . O . .
$ O X X X
$ O X O .
$ X X O X
$ . O X .
⍤⤙≍ "Black" Winner Board

# O wins crossing from top to bottom
Board ← $ . O . .
$ O X X X
$ O O O .
$ X X O X
$ . O X .
⍤⤙≍ "White" Winner Board

# X wins using a convoluted path
Board ← $ . X X . .
$ X . X . X
$ . X . X .
$ . X X . .
$ O O O O O
⍤⤙≍ "Black" Winner Board

# X wins using a spiral path
Board ← $ O X X X X X X X X
$ O X O O O O O O O
$ O X O X X X X X O
$ O X O X O O O X O
$ O X O X X X O X O
$ O X O O O X O X O
$ O X X X X X O X O
$ O O O O O O O X O
$ X X X X X X X X O
⍤⤙≍ "Black" Winner Board

0 comments on commit 52dcd8e

Please sign in to comment.