Skip to content

Commit 474843a

Browse files
Add dominoes
1 parent 4c972d0 commit 474843a

File tree

9 files changed

+221
-0
lines changed

9 files changed

+221
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,14 @@
903903
"prerequisites": [],
904904
"difficulty": 6
905905
},
906+
{
907+
"slug": "dominoes",
908+
"name": "Dominoes",
909+
"uuid": "9260be95-c9a5-4975-a00e-21828f5e9241",
910+
"practices": [],
911+
"prerequisites": [],
912+
"difficulty": 7
913+
},
906914
{
907915
"slug": "knapsack",
908916
"name": "Knapsack",
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Instructions Append
2+
3+
Each stone is represented as a dotted pair.
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.el"
8+
],
9+
"test": [
10+
"dominoes-test.el"
11+
],
12+
"example": [
13+
".meta/example.el"
14+
]
15+
},
16+
"blurb": "Make a chain of dominoes."
17+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
;;; dominoes.el --- Dominoes (exercism) -*- lexical-binding: t; -*-
2+
3+
;;; Commentary:
4+
5+
;;; Code:
6+
7+
(require 'cl-lib)
8+
9+
(require 'seq)
10+
11+
(defun evenp (number)
12+
(= (logand number 1) 0))
13+
14+
;; Each number is at most 6
15+
(defconst table-length 7)
16+
17+
(defun can-chain (dominoes)
18+
(letrec ((numbers (cl-loop for i below table-length collect i))
19+
(tally (make-vector table-length 0))
20+
(parent (vconcat numbers))
21+
(root (lambda (number)
22+
(if (= (aref parent number) number)
23+
number
24+
(funcall root (aref parent number)))))
25+
(process (lambda (stone)
26+
(aset tally (car stone) (1+ (aref tally (car stone))))
27+
(aset tally (cdr stone) (1+ (aref tally (cdr stone))))
28+
(aset parent (funcall root (cdr stone)) (funcall root (car stone)))))
29+
(rootp (lambda (number)
30+
(and (> (aref tally number) 0) (= (funcall root number) number)))))
31+
(mapc process dominoes)
32+
(and (seq-every-p #'evenp tally) (length< (seq-filter rootp numbers) 2))))
33+
34+
35+
(provide 'dominoes)
36+
;;; dominoes.el ends here
37+
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: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
;;; dominoes-test.el --- Tests for Dominoes (exercism) -*- lexical-binding: t; -*-
2+
3+
;;; Commentary:
4+
5+
;;; Code:
6+
7+
8+
(load-file "dominoes.el")
9+
(declare-function can-chain "dominoes.el" (dominoes))
10+
11+
12+
(ert-deftest empty-input-=-empty-output ()
13+
(should (can-chain [])))
14+
15+
16+
(ert-deftest singleton-input-=-singleton-output ()
17+
(should (can-chain [(1 . 1)])))
18+
19+
20+
(ert-deftest singleton-that-cant-be-chained ()
21+
(should-not (can-chain [(1 . 2)])))
22+
23+
24+
(ert-deftest three-elements ()
25+
(should (can-chain [(1 . 2) (3 . 1) (2 . 3)])))
26+
27+
28+
(ert-deftest can-reverse-dominoes ()
29+
(should (can-chain [(1 . 2) (1 . 3) (2 . 3)])))
30+
31+
32+
(ert-deftest cant-be-chained ()
33+
(should-not (can-chain [(1 . 2) (4 . 1) (2 . 3)])))
34+
35+
36+
(ert-deftest disconnected---simple ()
37+
(should-not (can-chain [(1 . 1) (2 . 2)])))
38+
39+
40+
(ert-deftest disconnected---double-loop ()
41+
(should-not (can-chain [(1 . 2) (2 . 1) (3 . 4) (4 . 3)])))
42+
43+
44+
(ert-deftest disconnected---single-isolated ()
45+
(should-not (can-chain [(1 . 2) (2 . 3) (3 . 1) (4 . 4)])))
46+
47+
48+
(ert-deftest need-backtrack ()
49+
(should (can-chain [(1 . 2) (2 . 3) (3 . 1) (2 . 4) (2 . 4)])))
50+
51+
52+
(ert-deftest separate-loops ()
53+
(should (can-chain [(1 . 2) (2 . 3) (3 . 1) (1 . 1) (2 . 2) (3 . 3)])))
54+
55+
56+
(ert-deftest nine-elements ()
57+
(should (can-chain [(1 . 2) (5 . 3) (3 . 1) (1 . 2) (2 . 4) (1 . 6) (2 . 3) (3 . 4) (5 . 6)])))
58+
59+
60+
(ert-deftest separate-three-domino-loops ()
61+
(should-not (can-chain [(1 . 2) (2 . 3) (3 . 1) (4 . 5) (5 . 6) (6 . 4)])))
62+
63+
64+
(provide 'dominoes-test)
65+
;;; dominoes-test.el ends here
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
;;; dominoes.el --- Dominoes (exercism) -*- lexical-binding: t; -*-
2+
3+
;;; Commentary:
4+
5+
;;; Code:
6+
7+
8+
(defun can-chain (dominoes)
9+
(error "Delete this S-Expression and write your own implementation"))
10+
11+
12+
(provide 'dominoes)
13+
;;; dominoes.el ends here
14+

0 commit comments

Comments
 (0)