From aa3defd30b78c2f3f3888996a8d3dcf4e696cb2c Mon Sep 17 00:00:00 2001 From: Eric Willigers Date: Thu, 10 Oct 2024 20:02:52 +1100 Subject: [PATCH] Add dominoes exercise --- config.json | 8 +++ .../practice/dominoes/.docs/instructions.md | 13 ++++ exercises/practice/dominoes/.meta/config.json | 17 +++++ exercises/practice/dominoes/.meta/example.v | 38 +++++++++++ exercises/practice/dominoes/.meta/tests.toml | 42 ++++++++++++ exercises/practice/dominoes/dominoes.v | 6 ++ exercises/practice/dominoes/run_test.v | 66 +++++++++++++++++++ 7 files changed, 190 insertions(+) create mode 100644 exercises/practice/dominoes/.docs/instructions.md create mode 100644 exercises/practice/dominoes/.meta/config.json create mode 100644 exercises/practice/dominoes/.meta/example.v create mode 100644 exercises/practice/dominoes/.meta/tests.toml create mode 100644 exercises/practice/dominoes/dominoes.v create mode 100644 exercises/practice/dominoes/run_test.v diff --git a/config.json b/config.json index a7748ca..7c97728 100644 --- a/config.json +++ b/config.json @@ -743,6 +743,14 @@ "prerequisites": [], "difficulty": 7 }, + { + "slug": "dominoes", + "name": "Dominoes", + "uuid": "62072c25-d963-467a-8bea-36b209a861e6", + "practices": [], + "prerequisites": [], + "difficulty": 7 + }, { "slug": "word-search", "name": "Word Search", diff --git a/exercises/practice/dominoes/.docs/instructions.md b/exercises/practice/dominoes/.docs/instructions.md new file mode 100644 index 0000000..1ced9f6 --- /dev/null +++ b/exercises/practice/dominoes/.docs/instructions.md @@ -0,0 +1,13 @@ +# Instructions + +Make a chain of dominoes. + +Compute a way to order a given set of dominoes in such a way that they form a correct domino chain (the dots on one half of a stone match the dots on the neighboring half of an adjacent stone) and that dots on the halves of the stones which don't have a neighbor (the first and last stone) 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. diff --git a/exercises/practice/dominoes/.meta/config.json b/exercises/practice/dominoes/.meta/config.json new file mode 100644 index 0000000..3dec9d7 --- /dev/null +++ b/exercises/practice/dominoes/.meta/config.json @@ -0,0 +1,17 @@ +{ + "authors": [ + "keiravillekode" + ], + "files": { + "solution": [ + "dominoes.v" + ], + "test": [ + "run_test.v" + ], + "example": [ + ".meta/example.v" + ] + }, + "blurb": "Make a chain of dominoes." +} diff --git a/exercises/practice/dominoes/.meta/example.v b/exercises/practice/dominoes/.meta/example.v new file mode 100644 index 0000000..9246014 --- /dev/null +++ b/exercises/practice/dominoes/.meta/example.v @@ -0,0 +1,38 @@ +module main + +type Domino = []int + +fn search(mut dominoes []Domino, remaining int) bool { + if remaining == 0 { + return dominoes[0][0] == dominoes[dominoes.len - 1][1] + } + + for i in 0..remaining { + if dominoes[i][0] == dominoes[remaining][0] { + dominoes[i][0], dominoes[i][1] = dominoes[i][1], dominoes[i][0] + } + + if dominoes[i][1] != dominoes[remaining][0] { + continue + } + + dominoes[i], dominoes[remaining - 1] = dominoes[remaining - 1], dominoes[i] + + if search(mut dominoes, remaining - 1) { + return true + } + + dominoes[i], dominoes[remaining - 1] = dominoes[remaining - 1], dominoes[i] + } + + return false +} + +fn can_chain(dominoes []Domino) bool { + if dominoes.len == 0 { + return true + } + + mut dominoes2 := dominoes.clone() + return search(mut dominoes2, dominoes.len - 1) +} diff --git a/exercises/practice/dominoes/.meta/tests.toml b/exercises/practice/dominoes/.meta/tests.toml new file mode 100644 index 0000000..d6164a0 --- /dev/null +++ b/exercises/practice/dominoes/.meta/tests.toml @@ -0,0 +1,42 @@ +# This is an auto-generated file. Regular comments will be removed when this +# file is regenerated. Regenerating will not touch any manually added keys, +# so comments can be added in 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" diff --git a/exercises/practice/dominoes/dominoes.v b/exercises/practice/dominoes/dominoes.v new file mode 100644 index 0000000..d86ce3e --- /dev/null +++ b/exercises/practice/dominoes/dominoes.v @@ -0,0 +1,6 @@ +module main + +type Domino = []int + +fn can_chain(dominoes []Domino) bool { +} diff --git a/exercises/practice/dominoes/run_test.v b/exercises/practice/dominoes/run_test.v new file mode 100644 index 0000000..b2d45a9 --- /dev/null +++ b/exercises/practice/dominoes/run_test.v @@ -0,0 +1,66 @@ +module main + +fn test_empty_input_empty_output() { + dominoes := [][]int{} + assert can_chain(dominoes) +} + +fn test_singleton_input_singleton_output() { + dominoes := [[1, 1]] + assert can_chain(dominoes) +} + +fn test_singleton_that_cant_be_chained() { + dominoes := [[1, 2]] + assert !can_chain(dominoes) +} + +fn test_three_elements() { + dominoes := [[1, 2], [3, 1], [2, 3]] + assert can_chain(dominoes) +} + +fn test_can_reverse_dominoes() { + dominoes := [[1, 2], [1, 3], [2, 3]] + assert can_chain(dominoes) +} + +fn test_cant_be_chained() { + dominoes := [[1, 2], [4, 1], [2, 3]] + assert !can_chain(dominoes) +} + +fn test_disconnected___simple() { + dominoes := [[1, 1], [2, 2]] + assert !can_chain(dominoes) +} + +fn test_disconnected___double_loop() { + dominoes := [[1, 2], [2, 1], [3, 4], [4, 3]] + assert !can_chain(dominoes) +} + +fn test_disconnected___single_isolated() { + dominoes := [[1, 2], [2, 3], [3, 1], [4, 4]] + assert !can_chain(dominoes) +} + +fn test_need_backtrack() { + dominoes := [[1, 2], [2, 3], [3, 1], [2, 4], [2, 4]] + assert can_chain(dominoes) +} + +fn test_separate_loops() { + dominoes := [[1, 2], [2, 3], [3, 1], [1, 1], [2, 2], [3, 3]] + assert can_chain(dominoes) +} + +fn test_nine_elements() { + dominoes := [[1, 2], [5, 3], [3, 1], [1, 2], [2, 4], [1, 6], [2, 3], [3, 4], [5, 6]] + assert can_chain(dominoes) +} + +fn test_separate_three_domino_loops() { + dominoes := [[1, 2], [2, 3], [3, 1], [4, 5], [5, 6], [6, 4]] + assert !can_chain(dominoes) +}