Skip to content

Commit 35059fe

Browse files
Add dominoes exercise (#356)
1 parent f6f9a66 commit 35059fe

8 files changed

Lines changed: 268 additions & 0 deletions

File tree

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,14 @@
891891
"prerequisites": [],
892892
"difficulty": 8
893893
},
894+
{
895+
"slug": "dominoes",
896+
"name": "Dominoes",
897+
"uuid": "93b688ab-47ae-4365-9f87-011e1ee174e1",
898+
"practices": [],
899+
"prerequisites": [],
900+
"difficulty": 8
901+
},
894902
{
895903
"slug": "zebra-puzzle",
896904
"name": "Zebra Puzzle",
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Instructions
2+
3+
Make a chain of dominoes.
4+
5+
Compute a way to order a given set of domino stones so that they form a correct domino chain.
6+
In the chain, the dots on one half of a stone must match the dots on the neighboring half of an adjacent stone.
7+
Additionally, the dots on the halves of the stones without neighbors (the first and last stone) must match each other.
8+
9+
For example given the stones `[2|1]`, `[2|3]` and `[1|3]` you should compute something
10+
like `[1|2] [2|3] [3|1]` or `[3|2] [2|1] [1|3]` or `[1|3] [3|2] [2|1]` etc, where the first and last numbers are the same.
11+
12+
For stones `[1|2]`, `[4|1]` and `[2|3]` the resulting chain is not valid: `[4|1] [1|2] [2|3]`'s first and last numbers are not the same.
13+
4 != 3
14+
15+
Some test cases may use duplicate stones in a chain solution, assume that multiple Domino sets are being used.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Introduction
2+
3+
In Toyland, the trains are always busy delivering treasures across the city, from shiny marbles to rare building blocks.
4+
The tracks they run on are made of colorful domino-shaped pieces, each marked with two numbers.
5+
For the trains to move, the dominoes must form a perfect chain where the numbers match.
6+
7+
Today, an urgent delivery of rare toys is on hold.
8+
You've been handed a set of track pieces to inspect.
9+
If they can form a continuous chain, the train will be on its way, bringing smiles across Toyland.
10+
If not, the set will be discarded, and another will be tried.
11+
12+
The toys are counting on you to solve this puzzle.
13+
Will the dominoes connect the tracks and send the train rolling, or will the set be left behind?
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"authors": [
3+
"keiravillekode"
4+
],
5+
"files": {
6+
"solution": [
7+
"source/dominoes.d"
8+
],
9+
"test": [
10+
"source/dominoes.d"
11+
],
12+
"example": [
13+
"example/dominoes.d"
14+
]
15+
},
16+
"blurb": "Make a chain of dominoes."
17+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[31a673f2-5e54-49fe-bd79-1c1dae476c9c]
13+
description = "empty input = empty output"
14+
15+
[4f99b933-367b-404b-8c6d-36d5923ee476]
16+
description = "singleton input = singleton output"
17+
18+
[91122d10-5ec7-47cb-b759-033756375869]
19+
description = "singleton that can't be chained"
20+
21+
[be8bc26b-fd3d-440b-8e9f-d698a0623be3]
22+
description = "three elements"
23+
24+
[99e615c6-c059-401c-9e87-ad7af11fea5c]
25+
description = "can reverse dominoes"
26+
27+
[51f0c291-5d43-40c5-b316-0429069528c9]
28+
description = "can't be chained"
29+
30+
[9a75e078-a025-4c23-8c3a-238553657f39]
31+
description = "disconnected - simple"
32+
33+
[0da0c7fe-d492-445d-b9ef-1f111f07a301]
34+
description = "disconnected - double loop"
35+
36+
[b6087ff0-f555-4ea0-a71c-f9d707c5994a]
37+
description = "disconnected - single isolated"
38+
39+
[2174fbdc-8b48-4bac-9914-8090d06ef978]
40+
description = "need backtrack"
41+
42+
[167bb480-dfd1-4318-a20d-4f90adb4a09f]
43+
description = "separate loops"
44+
45+
[cd061538-6046-45a7-ace9-6708fe8f6504]
46+
description = "nine elements"
47+
48+
[44704c7c-3adb-4d98-bd30-f45527cf8b49]
49+
description = "separate three-domino loops"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
name "dominoes"
2+
buildRequirements "disallowDeprecations"
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
module dominoes;
2+
3+
struct Stone {
4+
int left;
5+
int right;
6+
}
7+
8+
pure bool canChain(immutable Stone[] dominoes)
9+
{
10+
enum int n = 7; // values 0..6
11+
int[n] parent;
12+
int[n] rank;
13+
int[n] tally;
14+
15+
foreach (i; 0 .. n)
16+
parent[i] = i;
17+
18+
int find(int x)
19+
{
20+
while (parent[x] != x)
21+
{
22+
parent[x] = parent[parent[x]];
23+
x = parent[x];
24+
}
25+
return x;
26+
}
27+
28+
void unite(int a, int b)
29+
{
30+
int ra = find(a);
31+
int rb = find(b);
32+
if (ra == rb) return;
33+
34+
if (rank[ra] < rank[rb])
35+
{
36+
parent[ra] = rb;
37+
}
38+
else if (rank[ra] > rank[rb])
39+
{
40+
parent[rb] = ra;
41+
}
42+
else
43+
{
44+
parent[rb] = ra;
45+
rank[ra]++;
46+
}
47+
}
48+
49+
foreach (ref stone; dominoes)
50+
{
51+
tally[stone.left]++;
52+
tally[stone.right]++;
53+
unite(stone.left, stone.right);
54+
}
55+
56+
// All vertices must have even tally
57+
foreach (t; tally)
58+
if (t % 2 != 0) return false;
59+
60+
// At most one connected component among vertices with nonzero tally
61+
int roots = 0;
62+
foreach (i; 0 .. n)
63+
if (tally[i] > 0 && parent[i] == i)
64+
roots++;
65+
66+
return roots <= 1;
67+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
module dominoes;
2+
3+
struct Stone {
4+
int left;
5+
int right;
6+
}
7+
8+
pure bool canChain(immutable Stone[] dominoes)
9+
{
10+
// implement this function
11+
}
12+
13+
unittest
14+
{
15+
immutable int allTestsEnabled = 0;
16+
17+
// Empty input = empty output
18+
{
19+
immutable Stone[] dominoes;
20+
assert(canChain(dominoes));
21+
}
22+
23+
static if (allTestsEnabled)
24+
{
25+
// Singleton input = singleton output
26+
{
27+
immutable Stone[] dominoes = [Stone(1, 1)];
28+
assert(canChain(dominoes));
29+
}
30+
31+
// Singleton that can't be chained
32+
{
33+
immutable Stone[] dominoes = [Stone(1, 2)];
34+
assert(!canChain(dominoes));
35+
}
36+
37+
// Three elements
38+
{
39+
immutable Stone[] dominoes = [Stone(1, 2), Stone(3, 1), Stone(2, 3)];
40+
assert(canChain(dominoes));
41+
}
42+
43+
// Can reverse dominoes
44+
{
45+
immutable Stone[] dominoes = [Stone(1, 2), Stone(1, 3), Stone(2, 3)];
46+
assert(canChain(dominoes));
47+
}
48+
49+
// Can't be chained
50+
{
51+
immutable Stone[] dominoes = [Stone(1, 2), Stone(4, 1), Stone(2, 3)];
52+
assert(!canChain(dominoes));
53+
}
54+
55+
// Disconnected - simple
56+
{
57+
immutable Stone[] dominoes = [Stone(1, 1), Stone(2, 2)];
58+
assert(!canChain(dominoes));
59+
}
60+
61+
// Disconnected - double loop
62+
{
63+
immutable Stone[] dominoes = [Stone(1, 2), Stone(2, 1), Stone(3, 4), Stone(4, 3)];
64+
assert(!canChain(dominoes));
65+
}
66+
67+
// Disconnected - single isolated
68+
{
69+
immutable Stone[] dominoes = [Stone(1, 2), Stone(2, 3), Stone(3, 1), Stone(4, 4)];
70+
assert(!canChain(dominoes));
71+
}
72+
73+
// Need backtrack
74+
{
75+
immutable Stone[] dominoes = [Stone(1, 2), Stone(2, 3), Stone(3, 1), Stone(2, 4), Stone(2, 4)];
76+
assert(canChain(dominoes));
77+
}
78+
79+
// Separate loops
80+
{
81+
immutable Stone[] dominoes = [Stone(1, 2), Stone(2, 3), Stone(3, 1), Stone(1, 1), Stone(2, 2), Stone(3, 3)];
82+
assert(canChain(dominoes));
83+
}
84+
85+
// Nine elements
86+
{
87+
immutable Stone[] dominoes = [Stone(1, 2), Stone(5, 3), Stone(3, 1), Stone(1, 2), Stone(2, 4), Stone(1, 6), Stone(2, 3), Stone(3, 4), Stone(5, 6)];
88+
assert(canChain(dominoes));
89+
}
90+
91+
// Separate three-domino loops
92+
{
93+
immutable Stone[] dominoes = [Stone(1, 2), Stone(2, 3), Stone(3, 1), Stone(4, 5), Stone(5, 6), Stone(6, 4)];
94+
assert(!canChain(dominoes));
95+
}
96+
}
97+
}

0 commit comments

Comments
 (0)