Skip to content

Commit

Permalink
Add resistor-color-duo (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
keiravillekode authored Nov 9, 2024
1 parent ede7b92 commit 5cacf54
Show file tree
Hide file tree
Showing 12 changed files with 3,567 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,14 @@
"prerequisites": [],
"difficulty": 3
},
{
"slug": "resistor-color-duo",
"name": "Resistor Color Duo",
"uuid": "951a3e60-ce1d-4c08-b812-cd4198ed6fe1",
"practices": [],
"prerequisites": [],
"difficulty": 3
},
{
"slug": "scrabble-score",
"name": "Scrabble Score",
Expand Down
33 changes: 33 additions & 0 deletions exercises/practice/resistor-color-duo/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Instructions

If you want to build something using a Raspberry Pi, you'll probably use _resistors_.
For this exercise, you need to know two things about them:

- Each resistor has a resistance value.
- Resistors are small - so small in fact that if you printed the resistance value on them, it would be hard to read.

To get around this problem, manufacturers print color-coded bands onto the resistors to denote their resistance values.
Each band has a position and a numeric value.

The first 2 bands of a resistor have a simple encoding scheme: each color maps to a single number.
For example, if they printed a brown band (value 1) followed by a green band (value 5), it would translate to the number 15.

In this exercise you are going to create a helpful program so that you don't have to remember the values of the bands.
The program will take color names as input and output a two digit number, even if the input is more than two colors!

The band colors are encoded as follows:

- black: 0
- brown: 1
- red: 2
- orange: 3
- yellow: 4
- green: 5
- blue: 6
- violet: 7
- grey: 8
- white: 9

From the example above:
brown-green should return 15, and
brown-green-violet should return 15 too, ignoring the third color.
19 changes: 19 additions & 0 deletions exercises/practice/resistor-color-duo/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"keiravillekode"
],
"files": {
"solution": [
"resistor_color_duo.s"
],
"test": [
"resistor_color_duo_test.c"
],
"example": [
".meta/example.s"
]
},
"blurb": "Convert color codes, as used on resistors, to a numeric value.",
"source": "Maud de Vries, Erik Schierboom",
"source_url": "https://github.com/exercism/problem-specifications/issues/1464"
}
61 changes: 61 additions & 0 deletions exercises/practice/resistor-color-duo/.meta/example.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
.data
black: .string "black"
brown: .string "brown"
red: .string "red"
orange: .string "orange"
yellow: .string "yellow"
green: .string "green"
blue: .string "blue"
violet: .string "violet"
grey: .string "grey"
white: .string "white"

color_array:
.dword black
.dword brown
.dword red
.dword orange
.dword yellow
.dword green
.dword blue
.dword violet
.dword grey
.dword white
.dword 0 /* Sentinel value to indicate end of array */

.text
.globl value

color_code:
adrp x7, color_array
add x7, x7, :lo12:color_array
mov x2, x7

.next:
mov x3, x0
ldr x4, [x2], #8 /* load pointer, post-increment */

.compare:
ldrb w5, [x3], #1 /* load character, post-increment */
ldrb w6, [x4], #1 /* load character, post-increment */
cmp w5, w6
bne .next

cbnz w5, .compare

sub x2, x2, #8
sub x0, x2, x7
lsr x0, x0, #3
ret

/* extern int value(const char *first, const char *second, const char *third); */
value:
mov x9, lr
bl color_code
mov x11, x0
mov x0, x1
bl color_code
mov x10, #10
madd x0, x10, x11, x0
mov lr, x9
ret
31 changes: 31 additions & 0 deletions exercises/practice/resistor-color-duo/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# 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.

[ce11995a-5b93-4950-a5e9-93423693b2fc]
description = "Brown and black"

[7bf82f7a-af23-48ba-a97d-38d59406a920]
description = "Blue and grey"

[f1886361-fdfd-4693-acf8-46726fe24e0c]
description = "Yellow and violet"

[b7a6cbd2-ae3c-470a-93eb-56670b305640]
description = "White and red"

[77a8293d-2a83-4016-b1af-991acc12b9fe]
description = "Orange and orange"

[0c4fb44f-db7c-4d03-afa8-054350f156a8]
description = "Ignore additional colors"

[4a8ceec5-0ab4-4904-88a4-daf953a5e818]
description = "Black and brown, one-digit"
36 changes: 36 additions & 0 deletions exercises/practice/resistor-color-duo/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/resistor-color-duo/resistor_color_duo.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.text
.globl value

value:
ret
57 changes: 57 additions & 0 deletions exercises/practice/resistor-color-duo/resistor_color_duo_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include "vendor/unity.h"

#include <stddef.h>

extern int value(const char *first, const char *second, const char *third);

void setUp(void) {
}

void tearDown(void) {
}

void test_brown_and_black(void) {
TEST_ASSERT_EQUAL_INT(10, value("brown", "black", NULL));
}

void test_blue_and_grey(void) {
TEST_IGNORE();
TEST_ASSERT_EQUAL_INT(68, value("blue", "grey", NULL));
}

void test_yellow_and_violet(void) {
TEST_IGNORE();
TEST_ASSERT_EQUAL_INT(47, value("yellow", "violet", NULL));
}

void test_white_and_red(void) {
TEST_IGNORE();
TEST_ASSERT_EQUAL_INT(92, value("white", "red", NULL));
}

void test_orange_and_orange(void) {
TEST_IGNORE();
TEST_ASSERT_EQUAL_INT(33, value("orange", "orange", NULL));
}

void test_ignore_additional_colors(void) {
TEST_IGNORE();
TEST_ASSERT_EQUAL_INT(51, value("green", "brown", "orange"));
}

void test_black_and_brown_onedigit(void) {
TEST_IGNORE();
TEST_ASSERT_EQUAL_INT(1, value("black", "brown", NULL));
}

int main(void) {
UNITY_BEGIN();
RUN_TEST(test_brown_and_black);
RUN_TEST(test_blue_and_grey);
RUN_TEST(test_yellow_and_violet);
RUN_TEST(test_white_and_red);
RUN_TEST(test_orange_and_orange);
RUN_TEST(test_ignore_additional_colors);
RUN_TEST(test_black_and_brown_onedigit);
return UNITY_END();
}
Loading

0 comments on commit 5cacf54

Please sign in to comment.