Skip to content

Commit

Permalink
Add secret-handshake exercise (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
keiravillekode authored Nov 25, 2024
1 parent ba0cce6 commit 81f61d4
Show file tree
Hide file tree
Showing 14 changed files with 3,661 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,14 @@
"prerequisites": [],
"difficulty": 4
},
{
"slug": "secret-handshake",
"name": "Secret Handshake",
"uuid": "1d848a97-1f23-4d36-8b33-9f0f74da5c92",
"practices": [],
"prerequisites": [],
"difficulty": 4
},
{
"slug": "twelve-days",
"name": "Twelve Days",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Instructions append

## Output format

Output the sequence of actions as a null-terminated string, using a comma and a space as separators.
48 changes: 48 additions & 0 deletions exercises/practice/secret-handshake/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Instructions

Your task is to convert a number between 1 and 31 to a sequence of actions in the secret handshake.

The sequence of actions is chosen by looking at the rightmost five digits of the number once it's been converted to binary.
Start at the right-most digit and move left.

The actions for each number place are:

```plaintext
00001 = wink
00010 = double blink
00100 = close your eyes
01000 = jump
10000 = Reverse the order of the operations in the secret handshake.
```

Let's use the number `9` as an example:

- 9 in binary is `1001`.
- The digit that is farthest to the right is 1, so the first action is `wink`.
- Going left, the next digit is 0, so there is no double-blink.
- Going left again, the next digit is 0, so you leave your eyes open.
- Going left again, the next digit is 1, so you jump.

That was the last digit, so the final code is:

```plaintext
wink, jump
```

Given the number 26, which is `11010` in binary, we get the following actions:

- double blink
- jump
- reverse actions

The secret handshake for 26 is therefore:

```plaintext
jump, double blink
```

~~~~exercism/note
If you aren't sure what binary is or how it works, check out [this binary tutorial][intro-to-binary].
[intro-to-binary]: https://medium.com/basecs/bits-bytes-building-with-binary-13cb4289aafa
~~~~
7 changes: 7 additions & 0 deletions exercises/practice/secret-handshake/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Introduction

You are starting a secret coding club with some friends and friends-of-friends.
Not everyone knows each other, so you and your friends have decided to create a secret handshake that you can use to recognize that someone is a member.
You don't want anyone who isn't in the know to be able to crack the code.

You've designed the code so that one person says a number between 1 and 31, and the other person turns it into a series of actions.
19 changes: 19 additions & 0 deletions exercises/practice/secret-handshake/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"keiravillekode"
],
"files": {
"solution": [
"secret_handshake.s"
],
"test": [
"secret_handshake_test.c"
],
"example": [
".meta/example.s"
]
},
"blurb": "Given a decimal number, convert it to the appropriate sequence of events for a secret handshake.",
"source": "Bert, in Mary Poppins",
"source_url": "https://www.imdb.com/title/tt0058331/quotes/?item=qt0437047"
}
57 changes: 57 additions & 0 deletions exercises/practice/secret-handshake/.meta/example.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
.data
wink: .string "wink, "
double: .string "double blink, "
close: .string "close your eyes, "
jump: .string "jump, "

forwards:
.dword wink
.dword double
.dword close
.dword jump

backwards:
.dword jump
.dword close
.dword double
.dword wink

.text
.globl commands

/* extern void commands(char *buffer, int number); */
commands:
and x1, x1, #31
adrp x2, forwards
add x2, x2, :lo12:forwards
tst x1, #16
beq .start

adrp x2, backwards
add x2, x2, :lo12:backwards
rbit w1, w1 /* reverse bit order */
lsr w1, w1, #28

.start:
cbz x1, .empty

.check:
ldr x4, [x2], #8 /* address of command */
and x5, x1, #1
lsr x1, x1, #1
cbz x5, .check

.copy:
ldrb w3, [x4], #1
strb w3, [x0], #1
cbnz w3, .copy

sub x0, x0, #1 /* address of null terminator */
cbnz x1, .check

strb wzr, [x0, #-2] /* remove trailing ", " */
ret

.empty:
strb wzr, [x0]
ret
43 changes: 43 additions & 0 deletions exercises/practice/secret-handshake/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# 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.

[b8496fbd-6778-468c-8054-648d03c4bb23]
description = "wink for 1"

[83ec6c58-81a9-4fd1-bfaf-0160514fc0e3]
description = "double blink for 10"

[0e20e466-3519-4134-8082-5639d85fef71]
description = "close your eyes for 100"

[b339ddbb-88b7-4b7d-9b19-4134030d9ac0]
description = "jump for 1000"

[40499fb4-e60c-43d7-8b98-0de3ca44e0eb]
description = "combine two actions"

[9730cdd5-ef27-494b-afd3-5c91ad6c3d9d]
description = "reverse two actions"

[0b828205-51ca-45cd-90d5-f2506013f25f]
description = "reversing one action gives the same action"

[9949e2ac-6c9c-4330-b685-2089ab28b05f]
description = "reversing no actions still gives no actions"

[23fdca98-676b-4848-970d-cfed7be39f81]
description = "all possible actions"

[ae8fe006-d910-4d6f-be00-54b7c3799e79]
description = "reverse all possible actions"

[3d36da37-b31f-4cdb-a396-d93a2ee1c4a5]
description = "do nothing for zero"
36 changes: 36 additions & 0 deletions exercises/practice/secret-handshake/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/secret-handshake/secret_handshake.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.text
.globl commands

commands:
ret
114 changes: 114 additions & 0 deletions exercises/practice/secret-handshake/secret_handshake_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#include "vendor/unity.h"

#define BUFFER_SIZE 60

extern void commands(char *buffer, int number);

void setUp(void) {
}

void tearDown(void) {
}

void test_wink_for_1(void) {
char buffer[BUFFER_SIZE];

commands(buffer, 1);
TEST_ASSERT_EQUAL_STRING("wink", buffer);
}

void test_double_blink_for_10(void) {
TEST_IGNORE();
char buffer[BUFFER_SIZE];

commands(buffer, 2);
TEST_ASSERT_EQUAL_STRING("double blink", buffer);
}

void test_close_your_eyes_for_100(void) {
TEST_IGNORE();
char buffer[BUFFER_SIZE];

commands(buffer, 4);
TEST_ASSERT_EQUAL_STRING("close your eyes", buffer);
}

void test_jump_for_1000(void) {
TEST_IGNORE();
char buffer[BUFFER_SIZE];

commands(buffer, 8);
TEST_ASSERT_EQUAL_STRING("jump", buffer);
}

void test_combine_two_actions(void) {
TEST_IGNORE();
char buffer[BUFFER_SIZE];

commands(buffer, 3);
TEST_ASSERT_EQUAL_STRING("wink, double blink", buffer);
}

void test_reverse_two_actions(void) {
TEST_IGNORE();
char buffer[BUFFER_SIZE];

commands(buffer, 19);
TEST_ASSERT_EQUAL_STRING("double blink, wink", buffer);
}

void test_reversing_one_action_gives_the_same_action(void) {
TEST_IGNORE();
char buffer[BUFFER_SIZE];

commands(buffer, 24);
TEST_ASSERT_EQUAL_STRING("jump", buffer);
}

void test_reversing_no_actions_still_gives_no_actions(void) {
TEST_IGNORE();
char buffer[BUFFER_SIZE];

commands(buffer, 16);
TEST_ASSERT_EQUAL_STRING("", buffer);
}

void test_all_possible_actions(void) {
TEST_IGNORE();
char buffer[BUFFER_SIZE];

commands(buffer, 15);
TEST_ASSERT_EQUAL_STRING("wink, double blink, close your eyes, jump", buffer);
}

void test_reverse_all_possible_actions(void) {
TEST_IGNORE();
char buffer[BUFFER_SIZE];

commands(buffer, 31);
TEST_ASSERT_EQUAL_STRING("jump, close your eyes, double blink, wink", buffer);
}

void test_do_nothing_for_zero(void) {
TEST_IGNORE();
char buffer[BUFFER_SIZE];

commands(buffer, 0);
TEST_ASSERT_EQUAL_STRING("", buffer);
}

int main(void) {
UNITY_BEGIN();
RUN_TEST(test_wink_for_1);
RUN_TEST(test_double_blink_for_10);
RUN_TEST(test_close_your_eyes_for_100);
RUN_TEST(test_jump_for_1000);
RUN_TEST(test_combine_two_actions);
RUN_TEST(test_reverse_two_actions);
RUN_TEST(test_reversing_one_action_gives_the_same_action);
RUN_TEST(test_reversing_no_actions_still_gives_no_actions);
RUN_TEST(test_all_possible_actions);
RUN_TEST(test_reverse_all_possible_actions);
RUN_TEST(test_do_nothing_for_zero);
return UNITY_END();
}
Loading

0 comments on commit 81f61d4

Please sign in to comment.