Skip to content

Commit

Permalink
Add resistor-color-trio exercise (#125)
Browse files Browse the repository at this point in the history
We also modify the resistor-color-duo exercise so
that colors are passed as an array of strings.
  • Loading branch information
keiravillekode authored Dec 6, 2024
1 parent 9d4cdc9 commit d0bbccb
Show file tree
Hide file tree
Showing 15 changed files with 3,956 additions and 21 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,14 @@
"prerequisites": [],
"difficulty": 3
},
{
"slug": "resistor-color-trio",
"name": "Resistor Color Trio",
"uuid": "bae67e03-a8ce-4774-8dcf-342251eb4de9",
"practices": [],
"prerequisites": [],
"difficulty": 3
},
{
"slug": "scrabble-score",
"name": "Scrabble Score",
Expand Down
18 changes: 10 additions & 8 deletions exercises/practice/resistor-color-duo/.meta/example.s
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ color_array:
.globl value

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

.next:
mov x3, x0
Expand All @@ -44,18 +44,20 @@ color_code:
cbnz w5, .compare

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

/* extern int value(const char *first, const char *second, const char *third); */
/* extern int value(const char **colors); */
value:
mov x9, lr
mov x8, x0
mov x9, lr /* preserve return address */
mov x10, #10
ldr x0, [x8], #8 /* load address of first color */
bl color_code
mov x11, x0
mov x0, x1
ldr x0, [x8] /* load address of second color */
bl color_code
mov x10, #10
madd x0, x10, x11, x0
mov lr, x9
ret
52 changes: 44 additions & 8 deletions exercises/practice/resistor-color-duo/resistor_color_duo_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <stddef.h>

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

void setUp(void) {
}
Expand All @@ -11,37 +11,73 @@ void tearDown(void) {
}

void test_brown_and_black(void) {
TEST_ASSERT_EQUAL_INT(10, value("brown", "black", NULL));
const char *colors[] = {
"brown",
"black",
NULL
};
TEST_ASSERT_EQUAL_INT(10, value(colors));
}

void test_blue_and_grey(void) {
TEST_IGNORE();
TEST_ASSERT_EQUAL_INT(68, value("blue", "grey", NULL));
const char *colors[] = {
"blue",
"grey",
NULL
};
TEST_ASSERT_EQUAL_INT(68, value(colors));
}

void test_yellow_and_violet(void) {
TEST_IGNORE();
TEST_ASSERT_EQUAL_INT(47, value("yellow", "violet", NULL));
const char *colors[] = {
"yellow",
"violet",
NULL
};
TEST_ASSERT_EQUAL_INT(47, value(colors));
}

void test_white_and_red(void) {
TEST_IGNORE();
TEST_ASSERT_EQUAL_INT(92, value("white", "red", NULL));
const char *colors[] = {
"white",
"red",
NULL
};
TEST_ASSERT_EQUAL_INT(92, value(colors));
}

void test_orange_and_orange(void) {
TEST_IGNORE();
TEST_ASSERT_EQUAL_INT(33, value("orange", "orange", NULL));
const char *colors[] = {
"orange",
"orange",
NULL
};
TEST_ASSERT_EQUAL_INT(33, value(colors));
}

void test_ignore_additional_colors(void) {
TEST_IGNORE();
TEST_ASSERT_EQUAL_INT(51, value("green", "brown", "orange"));
const char *colors[] = {
"green",
"brown",
"orange",
NULL
};
TEST_ASSERT_EQUAL_INT(51, value(colors));
}

void test_black_and_brown_onedigit(void) {
TEST_IGNORE();
TEST_ASSERT_EQUAL_INT(1, value("black", "brown", NULL));
const char *colors[] = {
"black",
"brown",
NULL
};
TEST_ASSERT_EQUAL_INT(1, value(colors));
}

int main(void) {
Expand Down
56 changes: 56 additions & 0 deletions exercises/practice/resistor-color-trio/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Instructions

If you want to build something using a Raspberry Pi, you'll probably use _resistors_.
For this exercise, you need to know only three 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 acts as a digit of a 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 3 colors as input, and outputs the correct value, in ohms.
The color bands are encoded as follows:

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

In Resistor Color Duo you decoded the first two colors.
For instance: orange-orange got the main value `33`.
The third color stands for how many zeros need to be added to the main value.
The main value plus the zeros gives us a value in ohms.
For the exercise it doesn't matter what ohms really are.
For example:

- orange-orange-black would be 33 and no zeros, which becomes 33 ohms.
- orange-orange-red would be 33 and 2 zeros, which becomes 3300 ohms.
- orange-orange-orange would be 33 and 3 zeros, which becomes 33000 ohms.

(If Math is your thing, you may want to think of the zeros as exponents of 10.
If Math is not your thing, go with the zeros.
It really is the same thing, just in plain English instead of Math lingo.)

This exercise is about translating the colors into a label:

> "... ohms"
So an input of `"orange", "orange", "black"` should return:

> "33 ohms"
When we get to larger resistors, a [metric prefix][metric-prefix] is used to indicate a larger magnitude of ohms, such as "kiloohms".
That is similar to saying "2 kilometers" instead of "2000 meters", or "2 kilograms" for "2000 grams".

For example, an input of `"orange", "orange", "orange"` should return:

> "33 kiloohms"
[metric-prefix]: https://en.wikipedia.org/wiki/Metric_prefix
19 changes: 19 additions & 0 deletions exercises/practice/resistor-color-trio/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"keiravillekode"
],
"files": {
"solution": [
"resistor_color_trio.s"
],
"test": [
"resistor_color_trio_test.c"
],
"example": [
".meta/example.s"
]
},
"blurb": "Convert color codes, as used on resistors, to a human-readable label.",
"source": "Maud de Vries, Erik Schierboom",
"source_url": "https://github.com/exercism/problem-specifications/issues/1549"
}
124 changes: 124 additions & 0 deletions exercises/practice/resistor-color-trio/.meta/example.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
.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"

ohms: .string " ohms"
kiloohms:
.string " kiloohms"
megaohms:
.string " megaohms"
gigaohms:
.string " gigaohms"

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 */

units_array:
.dword ohms
.dword kiloohms
.dword megaohms
.dword gigaohms
.dword 0 /* Sentinel value to indicate end of array */

.text
.globl label

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

.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, x1
lsr x0, x0, #3
ret

/* extern void label(char *buffer, const char **colors); */
label:
mov x7, x0
mov x8, x1
mov x9, lr /* preserve return address */
mov x10, #10
ldr x0, [x8], #8 /* load address of first color */
bl color_code
mov w11, w0
ldr x0, [x8], #8 /* load address of second color */
bl color_code
mov w12, w0
ldr x0, [x8] /* load address of third color */
bl color_code
add w0, w0, #1
mov w3, #3
udiv w2, w0, w3 /* quotient, indicates units */
msub w4, w2, w3, w0 /* remainder */

cbz w4, .divide

cbz w11, .skip_first

add w11, w11, #'0'
strb w11, [x7], #1 /* write first digit */

.skip_first:
add w12, w12, #'0'
strb w12, [x7], #1 /* write second digit */
cmp w4, #1
beq .append_units

mov w12, #'0'
strb w12, [x7], #1 /* write zero */
b .append_units

.divide:
add w11, w11, #'0'
strb w11, [x7], #1 /* write first digit */
cbz w12, .append_units

mov w11, #'.'
strb w11, [x7], #1 /* write '.' */
add w12, w12, #'0'
strb w12, [x7], #1 /* write second digit */

.append_units:
adrp x1, units_array
add x1, x1, :lo12:units_array
lsl w2, w2, #3
ldr x2, [x1, x2]

.copy:
ldrb w3, [x2], #1
strb w3, [x7], #1
cbnz w3, .copy

mov lr, x9
ret
40 changes: 40 additions & 0 deletions exercises/practice/resistor-color-trio/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# 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.

[d6863355-15b7-40bb-abe0-bfb1a25512ed]
description = "Orange and orange and black"

[1224a3a9-8c8e-4032-843a-5224e04647d6]
description = "Blue and grey and brown"

[b8bda7dc-6b95-4539-abb2-2ad51d66a207]
description = "Red and black and red"

[5b1e74bc-d838-4eda-bbb3-eaba988e733b]
description = "Green and brown and orange"

[f5d37ef9-1919-4719-a90d-a33c5a6934c9]
description = "Yellow and violet and yellow"

[5f6404a7-5bb3-4283-877d-3d39bcc33854]
description = "Blue and violet and blue"

[7d3a6ab8-e40e-46c3-98b1-91639fff2344]
description = "Minimum possible value"

[ca0aa0ac-3825-42de-9f07-dac68cc580fd]
description = "Maximum possible value"

[0061a76c-903a-4714-8ce2-f26ce23b0e09]
description = "First two colors make an invalid octal number"

[30872c92-f567-4b69-a105-8455611c10c4]
description = "Ignore extra colors"
Loading

0 comments on commit d0bbccb

Please sign in to comment.