Skip to content

Commit

Permalink
Add rectangles exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
keiravillekode committed Nov 2, 2024
1 parent d530f47 commit 5a90f46
Show file tree
Hide file tree
Showing 12 changed files with 3,780 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,14 @@
"prerequisites": [],
"difficulty": 6
},
{
"slug": "rectangles",
"name": "Rectangles",
"uuid": "d06c5066-917e-454c-8549-3af3e8f7bd9f",
"practices": [],
"prerequisites": [],
"difficulty": 8
},
{
"slug": "zebra-puzzle",
"name": "Zebra Puzzle",
Expand Down
63 changes: 63 additions & 0 deletions exercises/practice/rectangles/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Instructions

Count the rectangles in an ASCII diagram like the one below.

```text
+--+
++ |
+-++--+
| | |
+--+--+
```

The above diagram contains these 6 rectangles:

```text
+-----+
| |
+-----+
```

```text
+--+
| |
| |
| |
+--+
```

```text
+--+
| |
+--+
```

```text
+--+
| |
+--+
```

```text
+--+
| |
+--+
```

```text
++
++
```

You may assume that the input is always a proper rectangle (i.e. the length of every line equals the length of the first line).
17 changes: 17 additions & 0 deletions exercises/practice/rectangles/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"authors": [
"keiravillekode"
],
"files": {
"solution": [
"rectangles.s"
],
"test": [
"rectangles_test.c"
],
"example": [
".meta/example.s"
]
},
"blurb": "Count the rectangles in an ASCII diagram."
}
85 changes: 85 additions & 0 deletions exercises/practice/rectangles/.meta/example.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
.text
.globl rectangles

/* int rectangles(const char **strings); */
rectangles:
mov x1, x0
mov x0, xzr /* initialize count */

.top_row:
ldr x2, [x1], #8 /* address of start of row */
cbz x2, .return

mov x3, x2 /* pointer into row */

.left_column:
ldrb w4, [x3], #1 /* load byte, post-increment */
cbz w4, .top_row

cmp w4, #'+'
bne .left_column

mov x4, x3

.right_column:
ldrb w5, [x4], #1 /* load byte, post-increment */
cmp w5, #'-'
beq .right_column

cmp w5, #'+'
bne .left_column

sub x5, x3, x2
sub x5, x5, #1 /* offset of left column from start of row */
sub x6, x4, x2
sub x6, x6, #1 /* offset of right column from start of row */
mov x7, x1

.bottom_row:
ldr x8, [x7], #8 /* address of start of row */
cbz x8, .right_column

mov w11, wzr /* number of | (0, 1 or 2) */
mov w12, wzr /* number of + (0, 1 or 2) */

ldrb w9, [x8, x5]
cmp w9, #'|'
cinc w11, w11, eq
cmp w9, #'+'
cinc w12, w12, eq

ldrb w9, [x8, x6]
cmp w9, #'|'
cinc w11, w11, eq
cmp w9, #'+'
cinc w12, w12, eq

add w11, w11, w12 /* number of | or + (0, 1 or 2) */
cmp w11, #2
bne .right_column

cmp w12, #2
bne .bottom_row

mov x11, x5 /* index into bottom row */

.scan_bottom:
add x11, x11, #1
cmp x11, x6
beq .accept

ldrb w9, [x8, x11]
cmp w9, #'-'
beq .scan_bottom

cmp w9, #'+'
beq .scan_bottom

b .bottom_row

.accept:
add x0, x0, #1
b .bottom_row

.return:
ret
52 changes: 52 additions & 0 deletions exercises/practice/rectangles/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# 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.

[485b7bab-4150-40aa-a8db-73013427d08c]
description = "no rows"

[076929ed-27e8-45dc-b14b-08279944dc49]
description = "no columns"

[0a8abbd1-a0a4-4180-aa4e-65c1b1a073fa]
description = "no rectangles"

[a4ba42e9-4e7f-4973-b7c7-4ce0760ac6cd]
description = "one rectangle"

[ced06550-83da-4d23-98b7-d24152e0db93]
description = "two rectangles without shared parts"

[5942d69a-a07c-41c8-8b93-2d13877c706a]
description = "five rectangles with shared parts"

[82d70be4-ab37-4bf2-a433-e33778d3bbf1]
description = "rectangle of height 1 is counted"

[57f1bc0e-2782-401e-ab12-7c01d8bfc2e0]
description = "rectangle of width 1 is counted"

[ef0bb65c-bd80-4561-9535-efc4067054f9]
description = "1x1 square is counted"

[e1e1d444-e926-4d30-9bf3-7d8ec9a9e330]
description = "only complete rectangles are counted"

[ca021a84-1281-4a56-9b9b-af14113933a4]
description = "rectangles can be of different sizes"

[51f689a7-ef3f-41ae-aa2f-5ea09ad897ff]
description = "corner is required for a rectangle to be complete"

[d78fe379-8c1b-4d3c-bdf7-29bfb6f6dc66]
description = "large input with many rectangles"

[6ef24e0f-d191-46da-b929-4faca24b4cd2]
description = "rectangles must have four sides"
36 changes: 36 additions & 0 deletions exercises/practice/rectangles/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/rectangles/rectangles.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.text
.globl rectangles

rectangles:
ret
Loading

0 comments on commit 5a90f46

Please sign in to comment.