Skip to content

Commit

Permalink
Add dominoes exercise (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
keiravillekode authored Dec 27, 2024
1 parent 43eedee commit 5a87770
Show file tree
Hide file tree
Showing 13 changed files with 3,681 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,14 @@
"prerequisites": [],
"difficulty": 8
},
{
"slug": "dominoes",
"name": "Dominoes",
"uuid": "58b0fc1e-db18-4b0b-bf31-058d8e6eb753",
"practices": [],
"prerequisites": [],
"difficulty": 9
},
{
"slug": "zebra-puzzle",
"name": "Zebra Puzzle",
Expand Down
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.s"
],
"test": [
"dominoes_test.c"
],
"example": [
".meta/example.s"
]
},
"blurb": "Make a chain of dominoes."
}
89 changes: 89 additions & 0 deletions exercises/practice/dominoes/.meta/example.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
.text
.globl can_chain

/* int root(int number, void*, uint8_t *table); */
root:
mov w9, w0
ldrb w0, [x2, x0]
cmp w0, w9
bne root

ret

/* void merge(size_t domino_count, const domino_t *dominoes, uint8_t *table); */
merge:
mov x14, lr /* preserve return address */
lsl w10, w0, #2 /* number of bytes occupied by dominoes array */

.merge_loop:
sub w10, w10, #2
ldrh w0, [x1, x10] /* number on domino half */
bl root
mov w11, w0

sub w10, w10, #2
ldrh w0, [x1, x10] /* number on domino half */
bl root

strb w0, [x2, x11] /* link domino halves in table */

cbnz w10, .merge_loop

ret x14

/* extern int can_chain(size_t domino_count, const domino_t *dominoes); */
can_chain:
stp xzr, xzr, [sp, #-16]!
mov x15, lr /* preserve return address */
cmp w0, wzr
beq .accept

mov x2, sp
lsl w9, w0, #2 /* number of bytes occupied by dominoes array */

.count_numbers:
sub w9, w9, #2
ldrh w10, [x1, x9] /* number on domino half */
ldrb w11, [x2, x10]
add w11, w11, #1 /* increment tally */
strb w11, [x2, x10]
cbnz w9, .count_numbers

mov w9, #16
mov w14, #255

.parity:
sub w9, w9, #1
ldrb w10, [sp, x9]
tst w10, #1 /* check if number appears an odd number of times */
bne .reject

tst w10, w10
csel w11, w14, w9, eq
strb w11, [sp, x9] /* populate disjoint set (union find) array */
cbnz w9, .parity

bl merge

mov w9, #16
mov w11, wzr

.count_roots:
sub w9, w9, #1
ldrb w10, [x2, x9]
cmp w10, w9
cinc w11, w11, eq
cbnz w9, .count_roots

cmp w11, #1
bne .reject

.accept:
mov w0, #1
add sp, sp, #16 /* restore stack pointer */
ret x15

.reject:
mov w0, wzr
add sp, sp, #16 /* restore stack pointer */
ret x15
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"
36 changes: 36 additions & 0 deletions exercises/practice/dominoes/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
AS = aarch64-linux-gnu-as
CC = aarch64-linux-gnu-gcc

CFLAGS = -g -Wall -Wextra -pedantic -Werror
LDFLAGS =

ALL_LDFLAGS = -pie -Wl,--fatal-warnings

ALL_CFLAGS = -std=c99 -fPIE $(CFLAGS)
ALL_LDFLAGS += $(LDFLAGS)

C_OBJS = $(patsubst %.c,%.o,$(wildcard *.c))
AS_OBJS = $(patsubst %.s,%.o,$(wildcard *.s))
ALL_OBJS = $(filter-out example.o,$(C_OBJS) $(AS_OBJS) vendor/unity.o)

CC_CMD = $(CC) $(ALL_CFLAGS) -c -o $@ $<

all: tests
qemu-aarch64 -L /usr/aarch64-linux-gnu ./$<

tests: $(ALL_OBJS)
@$(CC) $(ALL_CFLAGS) $(ALL_LDFLAGS) -o $@ $(ALL_OBJS)

%.o: %.s
@$(AS) -o $@ $<

%.o: %.c
@$(CC_CMD)

vendor/unity.o: vendor/unity.c vendor/unity.h vendor/unity_internals.h
@$(CC_CMD)

clean:
@rm -f *.o vendor/*.o tests

.PHONY: all clean
5 changes: 5 additions & 0 deletions exercises/practice/dominoes/dominoes.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.text
.globl can_chain

can_chain:
ret
113 changes: 113 additions & 0 deletions exercises/practice/dominoes/dominoes_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#include "vendor/unity.h"

#include <stddef.h>
#include <stdint.h>

#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))

typedef struct {
uint16_t first;
uint16_t second;
} domino_t;

extern int can_chain(size_t domino_count, const domino_t *dominoes);

void setUp(void) {
}

void tearDown(void) {
}

void test_empty_input__empty_output(void) {
TEST_ASSERT_TRUE(can_chain(0, NULL));
}

void test_singleton_input__singleton_output(void) {
TEST_IGNORE();
const domino_t dominoes[] = {{1, 1}};
TEST_ASSERT_TRUE(can_chain(ARRAY_SIZE(dominoes), dominoes));
}

void test_singleton_that_cant_be_chained(void) {
TEST_IGNORE();
const domino_t dominoes[] = {{1, 2}};
TEST_ASSERT_FALSE(can_chain(ARRAY_SIZE(dominoes), dominoes));
}

void test_three_elements(void) {
TEST_IGNORE();
const domino_t dominoes[] = {{1, 2}, {3, 1}, {2, 3}};
TEST_ASSERT_TRUE(can_chain(ARRAY_SIZE(dominoes), dominoes));
}

void test_can_reverse_dominoes(void) {
TEST_IGNORE();
const domino_t dominoes[] = {{1, 2}, {1, 3}, {2, 3}};
TEST_ASSERT_TRUE(can_chain(ARRAY_SIZE(dominoes), dominoes));
}

void test_cant_be_chained(void) {
TEST_IGNORE();
const domino_t dominoes[] = {{1, 2}, {4, 1}, {2, 3}};
TEST_ASSERT_FALSE(can_chain(ARRAY_SIZE(dominoes), dominoes));
}

void test_disconnected__simple(void) {
TEST_IGNORE();
const domino_t dominoes[] = {{1, 1}, {2, 2}};
TEST_ASSERT_FALSE(can_chain(ARRAY_SIZE(dominoes), dominoes));
}

void test_disconnected__double_loop(void) {
TEST_IGNORE();
const domino_t dominoes[] = {{1, 2}, {2, 1}, {3, 4}, {4, 3}};
TEST_ASSERT_FALSE(can_chain(ARRAY_SIZE(dominoes), dominoes));
}

void test_disconnected__single_isolated(void) {
TEST_IGNORE();
const domino_t dominoes[] = {{1, 2}, {2, 3}, {3, 1}, {4, 4}};
TEST_ASSERT_FALSE(can_chain(ARRAY_SIZE(dominoes), dominoes));
}

void test_need_backtrack(void) {
TEST_IGNORE();
const domino_t dominoes[] = {{1, 2}, {2, 3}, {3, 1}, {2, 4}, {2, 4}};
TEST_ASSERT_TRUE(can_chain(ARRAY_SIZE(dominoes), dominoes));
}

void test_separate_loops(void) {
TEST_IGNORE();
const domino_t dominoes[] = {{1, 2}, {2, 3}, {3, 1}, {1, 1}, {2, 2}, {3, 3}};
TEST_ASSERT_TRUE(can_chain(ARRAY_SIZE(dominoes), dominoes));
}

void test_nine_elements(void) {
TEST_IGNORE();
const domino_t dominoes[] = {{1, 2}, {5, 3}, {3, 1}, {1, 2}, {2, 4}, {1, 6}, {2, 3}, {3, 4}, {5, 6}};
TEST_ASSERT_TRUE(can_chain(ARRAY_SIZE(dominoes), dominoes));
}

void test_separate_threedomino_loops(void) {
TEST_IGNORE();
const domino_t dominoes[] = {{1, 2}, {2, 3}, {3, 1}, {4, 5}, {5, 6}, {6, 4}};
TEST_ASSERT_FALSE(can_chain(ARRAY_SIZE(dominoes), dominoes));
}

int main(void) {
UNITY_BEGIN();
RUN_TEST(test_empty_input__empty_output);
RUN_TEST(test_singleton_input__singleton_output);
RUN_TEST(test_singleton_that_cant_be_chained);
RUN_TEST(test_three_elements);
RUN_TEST(test_can_reverse_dominoes);
RUN_TEST(test_cant_be_chained);
RUN_TEST(test_disconnected__simple);
RUN_TEST(test_disconnected__double_loop);
RUN_TEST(test_disconnected__single_isolated);
RUN_TEST(test_need_backtrack);
RUN_TEST(test_separate_loops);
RUN_TEST(test_nine_elements);
RUN_TEST(test_separate_threedomino_loops);
return UNITY_END();
}
Loading

0 comments on commit 5a87770

Please sign in to comment.