Skip to content

Commit 775f1ea

Browse files
Add dominoes exercise (#61)
1 parent f93d953 commit 775f1ea

File tree

8 files changed

+198
-0
lines changed

8 files changed

+198
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,14 @@
433433
"prerequisites": [],
434434
"difficulty": 7
435435
},
436+
{
437+
"slug": "dominoes",
438+
"name": "Dominoes",
439+
"uuid": "4415c54b-162c-47df-a8c7-c47ff0effdb7",
440+
"practices": [],
441+
"prerequisites": [],
442+
"difficulty": 8
443+
},
436444
{
437445
"slug": "zebra-puzzle",
438446
"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+
"dominoes.fut"
8+
],
9+
"test": [
10+
"test.fut"
11+
],
12+
"example": [
13+
".meta/example.fut"
14+
]
15+
},
16+
"blurb": "Make a chain of dominoes."
17+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
def root (parents: []i64) (i: i64): i64 =
3+
let i = loop i = i while parents[i] != i do
4+
parents[i]
5+
in
6+
i
7+
8+
def can_chain [n] (dominoes: [n][2]i32): bool =
9+
let (tally, parents) = loop (tally, parents) = (replicate 7 0, iota 7) for stone in dominoes do
10+
let left = i64.i32 stone[0]
11+
let right = i64.i32 stone[1]
12+
let tally = tally with [left] = tally[left] + 1
13+
let tally = tally with [right] = tally[right] + 1
14+
let left = root parents left
15+
let right = root parents right
16+
let parents = parents with [right] = left
17+
in
18+
(tally, parents)
19+
in
20+
let count = loop count = 0 for i < 7 do
21+
if tally[i] % 2 != 0 then 7 else
22+
if tally[i] == 0 then count else
23+
if parents[i] != i then count else
24+
count + 1
25+
in
26+
count < 2
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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
def can_chain [n] (dominoes: [n][2]i32): bool = ???

exercises/practice/dominoes/test.fut

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import "dominoes"
2+
3+
-- empty input = empty output
4+
-- ==
5+
-- input { empty([0][2]i32) }
6+
-- output { true }
7+
8+
-- singleton input = singleton output
9+
-- ==
10+
-- input { [[1, 1]] }
11+
-- output { true }
12+
13+
-- singleton that can't be chained
14+
-- ==
15+
-- input { [[1, 2]] }
16+
-- output { false }
17+
18+
-- three elements
19+
-- ==
20+
-- input { [[1, 2], [3, 1], [2, 3]] }
21+
-- output { true }
22+
23+
-- can reverse dominoes
24+
-- ==
25+
-- input { [[1, 2], [1, 3], [2, 3]] }
26+
-- output { true }
27+
28+
-- can't be chained
29+
-- ==
30+
-- input { [[1, 2], [4, 1], [2, 3]] }
31+
-- output { false }
32+
33+
-- disconnected - simple
34+
-- ==
35+
-- input { [[1, 1], [2, 2]] }
36+
-- output { false }
37+
38+
-- disconnected - double loop
39+
-- ==
40+
-- input { [[1, 2], [2, 1], [3, 4], [4, 3]] }
41+
-- output { false }
42+
43+
-- disconnected - single isolated
44+
-- ==
45+
-- input { [[1, 2], [2, 3], [3, 1], [4, 4]] }
46+
-- output { false }
47+
48+
-- need backtrack
49+
-- ==
50+
-- input { [[1, 2], [2, 3], [3, 1], [2, 4], [2, 4]] }
51+
-- output { true }
52+
53+
-- separate loops
54+
-- ==
55+
-- input { [[1, 2], [2, 3], [3, 1], [1, 1], [2, 2], [3, 3]] }
56+
-- output { true }
57+
58+
-- nine elements
59+
-- ==
60+
-- input { [[1, 2], [5, 3], [3, 1], [1, 2], [2, 4], [1, 6], [2, 3], [3, 4], [5, 6]] }
61+
-- output { true }
62+
63+
-- separate three-domino loops
64+
-- ==
65+
-- input { [[1, 2], [2, 3], [3, 1], [4, 5], [5, 6], [6, 4]] }
66+
-- output { false }
67+
68+
let main [n] (dominoes: [n][2]i32): bool =
69+
can_chain dominoes

0 commit comments

Comments
 (0)