Skip to content

Commit cbfc718

Browse files
Add resistor-color-duo
1 parent ede7b92 commit cbfc718

File tree

12 files changed

+3567
-0
lines changed

12 files changed

+3567
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,14 @@
178178
"prerequisites": [],
179179
"difficulty": 3
180180
},
181+
{
182+
"slug": "resistor-color-duo",
183+
"name": "Resistor Color Duo",
184+
"uuid": "951a3e60-ce1d-4c08-b812-cd4198ed6fe1",
185+
"practices": [],
186+
"prerequisites": [],
187+
"difficulty": 3
188+
},
181189
{
182190
"slug": "scrabble-score",
183191
"name": "Scrabble Score",
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Instructions
2+
3+
If you want to build something using a Raspberry Pi, you'll probably use _resistors_.
4+
For this exercise, you need to know two things about them:
5+
6+
- Each resistor has a resistance value.
7+
- Resistors are small - so small in fact that if you printed the resistance value on them, it would be hard to read.
8+
9+
To get around this problem, manufacturers print color-coded bands onto the resistors to denote their resistance values.
10+
Each band has a position and a numeric value.
11+
12+
The first 2 bands of a resistor have a simple encoding scheme: each color maps to a single number.
13+
For example, if they printed a brown band (value 1) followed by a green band (value 5), it would translate to the number 15.
14+
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.
16+
The program will take color names as input and output a two digit number, even if the input is more than two colors!
17+
18+
The band colors are encoded as follows:
19+
20+
- black: 0
21+
- brown: 1
22+
- red: 2
23+
- orange: 3
24+
- yellow: 4
25+
- green: 5
26+
- blue: 6
27+
- violet: 7
28+
- grey: 8
29+
- white: 9
30+
31+
From the example above:
32+
brown-green should return 15, and
33+
brown-green-violet should return 15 too, ignoring the third color.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"keiravillekode"
4+
],
5+
"files": {
6+
"solution": [
7+
"resistor_color_duo.s"
8+
],
9+
"test": [
10+
"resistor_color_duo_test.c"
11+
],
12+
"example": [
13+
".meta/example.s"
14+
]
15+
},
16+
"blurb": "Convert color codes, as used on resistors, to a numeric value.",
17+
"source": "Maud de Vries, Erik Schierboom",
18+
"source_url": "https://github.com/exercism/problem-specifications/issues/1464"
19+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
.data
2+
black: .string "black"
3+
brown: .string "brown"
4+
red: .string "red"
5+
orange: .string "orange"
6+
yellow: .string "yellow"
7+
green: .string "green"
8+
blue: .string "blue"
9+
violet: .string "violet"
10+
grey: .string "grey"
11+
white: .string "white"
12+
13+
color_array:
14+
.dword black
15+
.dword brown
16+
.dword red
17+
.dword orange
18+
.dword yellow
19+
.dword green
20+
.dword blue
21+
.dword violet
22+
.dword grey
23+
.dword white
24+
.dword 0 /* Sentinel value to indicate end of array */
25+
26+
.text
27+
.globl value
28+
29+
color_code:
30+
adrp x7, color_array
31+
add x7, x7, :lo12:color_array
32+
mov x2, x7
33+
34+
.next:
35+
mov x3, x0
36+
ldr x4, [x2], #8 /* load pointer, post-increment */
37+
38+
.compare:
39+
ldrb w5, [x3], #1 /* load character, post-increment */
40+
ldrb w6, [x4], #1 /* load character, post-increment */
41+
cmp w5, w6
42+
bne .next
43+
44+
cbnz w5, .compare
45+
46+
sub x2, x2, #8
47+
sub x0, x2, x7
48+
lsr x0, x0, #3
49+
ret
50+
51+
/* extern int value(const char *first, const char *second, const char *third); */
52+
value:
53+
mov x9, lr
54+
bl color_code
55+
mov x11, x0
56+
mov x0, x1
57+
bl color_code
58+
mov x10, #10
59+
madd x0, x10, x11, x0
60+
mov lr, x9
61+
ret
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[ce11995a-5b93-4950-a5e9-93423693b2fc]
13+
description = "Brown and black"
14+
15+
[7bf82f7a-af23-48ba-a97d-38d59406a920]
16+
description = "Blue and grey"
17+
18+
[f1886361-fdfd-4693-acf8-46726fe24e0c]
19+
description = "Yellow and violet"
20+
21+
[b7a6cbd2-ae3c-470a-93eb-56670b305640]
22+
description = "White and red"
23+
24+
[77a8293d-2a83-4016-b1af-991acc12b9fe]
25+
description = "Orange and orange"
26+
27+
[0c4fb44f-db7c-4d03-afa8-054350f156a8]
28+
description = "Ignore additional colors"
29+
30+
[4a8ceec5-0ab4-4904-88a4-daf953a5e818]
31+
description = "Black and brown, one-digit"
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
AS = aarch64-linux-gnu-as
2+
CC = aarch64-linux-gnu-gcc
3+
4+
CFLAGS = -g -Wall -Wextra -pedantic -Werror
5+
LDFLAGS =
6+
7+
ALL_LDFLAGS = -pie -Wl,--fatal-warnings
8+
9+
ALL_CFLAGS = -std=c99 -fPIE $(CFLAGS)
10+
ALL_LDFLAGS += $(LDFLAGS)
11+
12+
C_OBJS = $(patsubst %.c,%.o,$(wildcard *.c))
13+
AS_OBJS = $(patsubst %.s,%.o,$(wildcard *.s))
14+
ALL_OBJS = $(filter-out example.o,$(C_OBJS) $(AS_OBJS) vendor/unity.o)
15+
16+
CC_CMD = $(CC) $(ALL_CFLAGS) -c -o $@ $<
17+
18+
all: tests
19+
qemu-aarch64 -L /usr/aarch64-linux-gnu ./$<
20+
21+
tests: $(ALL_OBJS)
22+
@$(CC) $(ALL_CFLAGS) $(ALL_LDFLAGS) -o $@ $(ALL_OBJS)
23+
24+
%.o: %.s
25+
@$(AS) -o $@ $<
26+
27+
%.o: %.c
28+
@$(CC_CMD)
29+
30+
vendor/unity.o: vendor/unity.c vendor/unity.h vendor/unity_internals.h
31+
@$(CC_CMD)
32+
33+
clean:
34+
@rm -f *.o vendor/*.o tests
35+
36+
.PHONY: all clean
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.text
2+
.globl value
3+
4+
value:
5+
ret
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include "vendor/unity.h"
2+
3+
#include <stddef.h>
4+
5+
extern int value(const char *first, const char *second, const char *third);
6+
7+
void setUp(void) {
8+
}
9+
10+
void tearDown(void) {
11+
}
12+
13+
void test_brown_and_black(void) {
14+
TEST_ASSERT_EQUAL_INT(10, value("brown", "black", NULL));
15+
}
16+
17+
void test_blue_and_grey(void) {
18+
TEST_IGNORE();
19+
TEST_ASSERT_EQUAL_INT(68, value("blue", "grey", NULL));
20+
}
21+
22+
void test_yellow_and_violet(void) {
23+
TEST_IGNORE();
24+
TEST_ASSERT_EQUAL_INT(47, value("yellow", "violet", NULL));
25+
}
26+
27+
void test_white_and_red(void) {
28+
TEST_IGNORE();
29+
TEST_ASSERT_EQUAL_INT(92, value("white", "red", NULL));
30+
}
31+
32+
void test_orange_and_orange(void) {
33+
TEST_IGNORE();
34+
TEST_ASSERT_EQUAL_INT(33, value("orange", "orange", NULL));
35+
}
36+
37+
void test_ignore_additional_colors(void) {
38+
TEST_IGNORE();
39+
TEST_ASSERT_EQUAL_INT(51, value("green", "brown", "orange"));
40+
}
41+
42+
void test_black_and_brown_onedigit(void) {
43+
TEST_IGNORE();
44+
TEST_ASSERT_EQUAL_INT(1, value("black", "brown", NULL));
45+
}
46+
47+
int main(void) {
48+
UNITY_BEGIN();
49+
RUN_TEST(test_brown_and_black);
50+
RUN_TEST(test_blue_and_grey);
51+
RUN_TEST(test_yellow_and_violet);
52+
RUN_TEST(test_white_and_red);
53+
RUN_TEST(test_orange_and_orange);
54+
RUN_TEST(test_ignore_additional_colors);
55+
RUN_TEST(test_black_and_brown_onedigit);
56+
return UNITY_END();
57+
}

0 commit comments

Comments
 (0)