Skip to content

Commit 34453b7

Browse files
Add food-chain
1 parent d84b43b commit 34453b7

File tree

12 files changed

+3876
-0
lines changed

12 files changed

+3876
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,14 @@
391391
"prerequisites": [],
392392
"difficulty": 6
393393
},
394+
{
395+
"slug": "food-chain",
396+
"name": "Food Chain",
397+
"uuid": "26858c8a-42a9-43a7-9c94-503138d1c615",
398+
"practices": [],
399+
"prerequisites": [],
400+
"difficulty": 6
401+
},
394402
{
395403
"slug": "knapsack",
396404
"name": "Knapsack",
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Instructions
2+
3+
Generate the lyrics of the song 'I Know an Old Lady Who Swallowed a Fly'.
4+
5+
While you could copy/paste the lyrics, or read them from a file, this problem is much more interesting if you approach it algorithmically.
6+
7+
This is a [cumulative song][cumulative-song] of unknown origin.
8+
9+
This is one of many common variants.
10+
11+
```text
12+
I know an old lady who swallowed a fly.
13+
I don't know why she swallowed the fly. Perhaps she'll die.
14+
15+
I know an old lady who swallowed a spider.
16+
It wriggled and jiggled and tickled inside her.
17+
She swallowed the spider to catch the fly.
18+
I don't know why she swallowed the fly. Perhaps she'll die.
19+
20+
I know an old lady who swallowed a bird.
21+
How absurd to swallow a bird!
22+
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
23+
She swallowed the spider to catch the fly.
24+
I don't know why she swallowed the fly. Perhaps she'll die.
25+
26+
I know an old lady who swallowed a cat.
27+
Imagine that, to swallow a cat!
28+
She swallowed the cat to catch the bird.
29+
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
30+
She swallowed the spider to catch the fly.
31+
I don't know why she swallowed the fly. Perhaps she'll die.
32+
33+
I know an old lady who swallowed a dog.
34+
What a hog, to swallow a dog!
35+
She swallowed the dog to catch the cat.
36+
She swallowed the cat to catch the bird.
37+
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
38+
She swallowed the spider to catch the fly.
39+
I don't know why she swallowed the fly. Perhaps she'll die.
40+
41+
I know an old lady who swallowed a goat.
42+
Just opened her throat and swallowed a goat!
43+
She swallowed the goat to catch the dog.
44+
She swallowed the dog to catch the cat.
45+
She swallowed the cat to catch the bird.
46+
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
47+
She swallowed the spider to catch the fly.
48+
I don't know why she swallowed the fly. Perhaps she'll die.
49+
50+
I know an old lady who swallowed a cow.
51+
I don't know how she swallowed a cow!
52+
She swallowed the cow to catch the goat.
53+
She swallowed the goat to catch the dog.
54+
She swallowed the dog to catch the cat.
55+
She swallowed the cat to catch the bird.
56+
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
57+
She swallowed the spider to catch the fly.
58+
I don't know why she swallowed the fly. Perhaps she'll die.
59+
60+
I know an old lady who swallowed a horse.
61+
She's dead, of course!
62+
```
63+
64+
[cumulative-song]: https://en.wikipedia.org/wiki/Cumulative_song
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+
"food_chain.s"
8+
],
9+
"test": [
10+
"food_chain_test.c"
11+
],
12+
"example": [
13+
".meta/example.s"
14+
]
15+
},
16+
"blurb": "Generate the lyrics of the song 'I Know an Old Lady Who Swallowed a Fly'.",
17+
"source": "Wikipedia",
18+
"source_url": "https://en.wikipedia.org/wiki/There_Was_an_Old_Lady_Who_Swallowed_a_Fly"
19+
}
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
.section .rodata
2+
3+
i_know: .string "I know an old lady who swallowed a "
4+
stop: .string ".\n"
5+
she_swallowed:
6+
.string "She swallowed the "
7+
to_catch:
8+
.string " to catch the "
9+
that_wriggled:
10+
.string " that wriggled and jiggled and tickled inside her"
11+
12+
animals:
13+
.string "flyspiderbirdcatdoggoatcowhorse"
14+
15+
animals_table:
16+
.hword 0, 0, 3, 9, 13, 16, 19, 23, 26, 31, 31
17+
18+
exclamations:
19+
.string "I don't know why she swallowed the fly. Perhaps she'll die.\nIt wriggled and jiggled and tickled inside her.\nHow absurd to swallow a bird!\nImagine that, to swallow a cat!\nWhat a hog, to swallow a dog!\nJust opened her throat and swallowed a goat!\nI don't know how she swallowed a cow!\nShe's dead, of course!\n"
20+
21+
exclamations_table:
22+
.hword 0, 0, 60, 108, 138, 170, 200, 245, 283, 306
23+
24+
25+
.text
26+
.globl recite
27+
28+
29+
/* char *copy(char *dest, void *, void *, const char *source); */
30+
copy:
31+
lb t0, 0(a3)
32+
addi a3, a3, 1
33+
sb t0, 0(a0)
34+
addi a0, a0, 1
35+
bnez t0, copy
36+
37+
addi a0, a0, -1
38+
jalr zero, 0(t6) /* return */
39+
40+
41+
/* char *copy_n(char *dest, void *, void *, const char *source, size_t length); */
42+
copy_n:
43+
beqz a4, .copy_n_return
44+
45+
lb t0, 0(a3)
46+
addi a3, a3, 1
47+
sb t0, 0(a0)
48+
addi a0, a0, 1
49+
addi a4, a4, -1
50+
j copy_n
51+
52+
.copy_n_return:
53+
jalr zero, 0(t6) /* return */
54+
55+
56+
/* extern void recite(char *buffer, int start_verse, int end_verse); */
57+
recite:
58+
slli a1, a1, 1 /* multiply start_verse by 2 */
59+
slli a2, a2, 1 /* multiply end_verse by 2 */
60+
li a7, 2 /* fly */
61+
li a6, 4 /* spider */
62+
li a5, '\n'
63+
64+
.stanza:
65+
la a3, i_know
66+
jal t6, copy /* subroutine call */
67+
68+
la a3, animals
69+
la t0, animals_table
70+
add t0, t0, a1
71+
lhu t1, 0(t0)
72+
add a3, a3, t1 /* start of animal string */
73+
addi t0, t0, 2
74+
lhu t2, 0(t0)
75+
sub a4, t2, t1 /* length of animal string */
76+
jal t6, copy_n /* subroutine call */
77+
78+
la a3, stop
79+
jal t6, copy /* subroutine call */
80+
81+
la a3, exclamations
82+
la t0, exclamations_table
83+
add t0, t0, a1
84+
lhu t1, 0(t0)
85+
add a3, a3, t1 /* start of exclamation string */
86+
addi t0, t0, 2
87+
lhu t2, 0(t0)
88+
sub a4, t2, t1 /* length of exclamation string */
89+
jal t6, copy_n /* subroutine call */
90+
91+
li t0, 2
92+
beq a1, t0, .next /* first verse? */
93+
94+
li t0, 16
95+
beq a1, t0, .next /* last verse? */
96+
97+
move t3, a1 /* current animal */
98+
99+
.explain:
100+
la a3, she_swallowed
101+
jal t6, copy /* subroutine call */
102+
103+
la a3, animals
104+
la t0, animals_table
105+
add t0, t0, t3
106+
lhu t1, 0(t0)
107+
add a3, a3, t1 /* start of animal string */
108+
addi t0, t0, 2
109+
lhu t2, 0(t0)
110+
sub a4, t2, t1 /* length of animal string */
111+
jal t6, copy_n /* subroutine call */
112+
113+
addi t3, t3, -2 /* previous animal */
114+
115+
la a3, to_catch
116+
jal t6, copy /* subroutine call */
117+
118+
la a3, animals
119+
la t0, animals_table
120+
add t0, t0, t3
121+
lhu t1, 0(t0)
122+
add a3, a3, t1 /* start of animal string */
123+
addi t0, t0, 2
124+
lhu t2, 0(t0)
125+
sub a4, t2, t1 /* length of animal string */
126+
jal t6, copy_n /* subroutine call */
127+
128+
bne t3, a6, .stop /* only spider wriggles */
129+
130+
la a3, that_wriggled
131+
jal t6, copy /* subroutine call */
132+
133+
.stop:
134+
la a3, stop
135+
jal t6, copy /* subroutine call */
136+
137+
bne t3, a7, .explain /* loop until fly */
138+
139+
la a3, exclamations
140+
la t0, exclamations_table
141+
add t0, t0, t3
142+
lhu t1, 0(t0)
143+
add a3, a3, t1 /* start of exclamation string */
144+
addi t0, t0, 2
145+
lhu t2, 0(t0)
146+
sub a4, t2, t1 /* length of exclamation string */
147+
jal t6, copy_n /* subroutine call */
148+
149+
.next:
150+
sb a5, 0(a0) /* newline */
151+
addi a0, a0, 1
152+
addi a1, a1, 2
153+
ble a1, a2, .stanza
154+
155+
sb zero, -1(a0)
156+
ret
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
[751dce68-9412-496e-b6e8-855998c56166]
13+
description = "fly"
14+
15+
[6c56f861-0c5e-4907-9a9d-b2efae389379]
16+
description = "spider"
17+
18+
[3edf5f33-bef1-4e39-ae67-ca5eb79203fa]
19+
description = "bird"
20+
21+
[e866a758-e1ff-400e-9f35-f27f28cc288f]
22+
description = "cat"
23+
24+
[3f02c30e-496b-4b2a-8491-bc7e2953cafb]
25+
description = "dog"
26+
27+
[4b3fd221-01ea-46e0-825b-5734634fbc59]
28+
description = "goat"
29+
30+
[1b707da9-7001-4fac-941f-22ad9c7a65d4]
31+
description = "cow"
32+
33+
[3cb10d46-ae4e-4d2c-9296-83c9ffc04cdc]
34+
description = "horse"
35+
36+
[22b863d5-17e4-4d1e-93e4-617329a5c050]
37+
description = "multiple verses"
38+
39+
[e626b32b-745c-4101-bcbd-3b13456893db]
40+
description = "full song"
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
AS = /usr/bin/riscv64-linux-gnu-as
2+
CC = /usr/bin/riscv64-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-riscv64 -L /usr/riscv64-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 recite
3+
4+
recite:
5+
ret

0 commit comments

Comments
 (0)