Skip to content

Add dominoes #488

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,14 @@
"prerequisites": [],
"difficulty": 6
},
{
"slug": "dominoes",
"name": "Dominoes",
"uuid": "9260be95-c9a5-4975-a00e-21828f5e9241",
"practices": [],
"prerequisites": [],
"difficulty": 7
},
{
"slug": "knapsack",
"name": "Knapsack",
Expand Down
3 changes: 3 additions & 0 deletions exercises/practice/dominoes/.docs/instructions.append.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Instructions Append

Each stone is represented as a dotted pair.
15 changes: 15 additions & 0 deletions exercises/practice/dominoes/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Instructions

Make a chain of dominoes.

Compute a way to order a given set of domino stones so that they form a correct domino chain.
In the chain, the dots on one half of a stone must match the dots on the neighboring half of an adjacent stone.
Additionally, the dots on the halves of the stones without neighbors (the first and last stone) must match each other.

For example given the stones `[2|1]`, `[2|3]` and `[1|3]` you should compute something
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.

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.
4 != 3

Some test cases may use duplicate stones in a chain solution, assume that multiple Domino sets are being used.
13 changes: 13 additions & 0 deletions exercises/practice/dominoes/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Introduction

In Toyland, the trains are always busy delivering treasures across the city, from shiny marbles to rare building blocks.
The tracks they run on are made of colorful domino-shaped pieces, each marked with two numbers.
For the trains to move, the dominoes must form a perfect chain where the numbers match.

Today, an urgent delivery of rare toys is on hold.
You've been handed a set of track pieces to inspect.
If they can form a continuous chain, the train will be on its way, bringing smiles across Toyland.
If not, the set will be discarded, and another will be tried.

The toys are counting on you to solve this puzzle.
Will the dominoes connect the tracks and send the train rolling, or will the set be left behind?
17 changes: 17 additions & 0 deletions exercises/practice/dominoes/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"authors": [
"keiravillekode"
],
"files": {
"solution": [
"dominoes.el"
],
"test": [
"dominoes-test.el"
],
"example": [
".meta/example.el"
]
},
"blurb": "Make a chain of dominoes."
}
34 changes: 34 additions & 0 deletions exercises/practice/dominoes/.meta/example.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
;;; dominoes.el --- Dominoes (exercism) -*- lexical-binding: t; -*-

;;; Commentary:

;;; Code:

(require 'cl-lib)
(require 'seq)

(defun evenp (number)
(= (logand number 1) 0))

(defun can-chain (dominoes)
(letrec ((table-length 7)
(numbers (cl-loop for i below table-length collect i))
(tally (make-vector table-length 0))
(parent (vconcat numbers))
(root (lambda (number)
(if (= (aref parent number) number)
number
(funcall root (aref parent number)))))
(process (lambda (stone)
(aset tally (car stone) (1+ (aref tally (car stone))))
(aset tally (cdr stone) (1+ (aref tally (cdr stone))))
(aset parent (funcall root (cdr stone)) (funcall root (car stone)))))
(rootp (lambda (number)
(and (> (aref tally number) 0) (= (funcall root number) number)))))
(mapc process dominoes)
(and (seq-every-p #'evenp tally) (length< (seq-filter rootp numbers) 2))))


(provide 'dominoes)
;;; dominoes.el ends here

49 changes: 49 additions & 0 deletions exercises/practice/dominoes/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# 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.

[31a673f2-5e54-49fe-bd79-1c1dae476c9c]
description = "empty input = empty output"

[4f99b933-367b-404b-8c6d-36d5923ee476]
description = "singleton input = singleton output"

[91122d10-5ec7-47cb-b759-033756375869]
description = "singleton that can't be chained"

[be8bc26b-fd3d-440b-8e9f-d698a0623be3]
description = "three elements"

[99e615c6-c059-401c-9e87-ad7af11fea5c]
description = "can reverse dominoes"

[51f0c291-5d43-40c5-b316-0429069528c9]
description = "can't be chained"

[9a75e078-a025-4c23-8c3a-238553657f39]
description = "disconnected - simple"

[0da0c7fe-d492-445d-b9ef-1f111f07a301]
description = "disconnected - double loop"

[b6087ff0-f555-4ea0-a71c-f9d707c5994a]
description = "disconnected - single isolated"

[2174fbdc-8b48-4bac-9914-8090d06ef978]
description = "need backtrack"

[167bb480-dfd1-4318-a20d-4f90adb4a09f]
description = "separate loops"

[cd061538-6046-45a7-ace9-6708fe8f6504]
description = "nine elements"

[44704c7c-3adb-4d98-bd30-f45527cf8b49]
description = "separate three-domino loops"
65 changes: 65 additions & 0 deletions exercises/practice/dominoes/dominoes-test.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
;;; dominoes-test.el --- Tests for Dominoes (exercism) -*- lexical-binding: t; -*-

;;; Commentary:

;;; Code:


(load-file "dominoes.el")
(declare-function can-chain "dominoes.el" (dominoes))


(ert-deftest empty-input-=-empty-output ()
(should (can-chain [])))


(ert-deftest singleton-input-=-singleton-output ()
(should (can-chain [(1 . 1)])))


(ert-deftest singleton-that-cant-be-chained ()
(should-not (can-chain [(1 . 2)])))


(ert-deftest three-elements ()
(should (can-chain [(1 . 2) (3 . 1) (2 . 3)])))


(ert-deftest can-reverse-dominoes ()
(should (can-chain [(1 . 2) (1 . 3) (2 . 3)])))


(ert-deftest cant-be-chained ()
(should-not (can-chain [(1 . 2) (4 . 1) (2 . 3)])))


(ert-deftest disconnected---simple ()
(should-not (can-chain [(1 . 1) (2 . 2)])))


(ert-deftest disconnected---double-loop ()
(should-not (can-chain [(1 . 2) (2 . 1) (3 . 4) (4 . 3)])))


(ert-deftest disconnected---single-isolated ()
(should-not (can-chain [(1 . 2) (2 . 3) (3 . 1) (4 . 4)])))


(ert-deftest need-backtrack ()
(should (can-chain [(1 . 2) (2 . 3) (3 . 1) (2 . 4) (2 . 4)])))


(ert-deftest separate-loops ()
(should (can-chain [(1 . 2) (2 . 3) (3 . 1) (1 . 1) (2 . 2) (3 . 3)])))


(ert-deftest nine-elements ()
(should (can-chain [(1 . 2) (5 . 3) (3 . 1) (1 . 2) (2 . 4) (1 . 6) (2 . 3) (3 . 4) (5 . 6)])))


(ert-deftest separate-three-domino-loops ()
(should-not (can-chain [(1 . 2) (2 . 3) (3 . 1) (4 . 5) (5 . 6) (6 . 4)])))


(provide 'dominoes-test)
;;; dominoes-test.el ends here
14 changes: 14 additions & 0 deletions exercises/practice/dominoes/dominoes.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
;;; dominoes.el --- Dominoes (exercism) -*- lexical-binding: t; -*-

;;; Commentary:

;;; Code:


(defun can-chain (dominoes)
(error "Delete this S-Expression and write your own implementation"))


(provide 'dominoes)
;;; dominoes.el ends here