Skip to content

Commit 8684e99

Browse files
Add game-of-life
1 parent c437e96 commit 8684e99

File tree

14 files changed

+3894
-0
lines changed

14 files changed

+3894
-0
lines changed

config.json

+8
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,14 @@
517517
"prerequisites": [],
518518
"difficulty": 8
519519
},
520+
{
521+
"slug": "game-of-life",
522+
"name": "Conway's Game of Life",
523+
"uuid": "5e290392-ad3b-42a7-8612-9f22a69dfeaf",
524+
"practices": [],
525+
"prerequisites": [],
526+
"difficulty": 8
527+
},
520528
{
521529
"slug": "meetup",
522530
"name": "Meetup",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Instructions append
2+
3+
Each row of the grid is represented as a 64 bit integer.
4+
5+
For example,
6+
7+
```
8+
0 1 0
9+
1 0 0
10+
1 1 0
11+
```
12+
13+
is represented as
14+
15+
```
16+
2
17+
4
18+
6
19+
```
20+
21+
The grid has at most 64 columns.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Instructions
2+
3+
After each generation, the cells interact with their eight neighbors, which are cells adjacent horizontally, vertically, or diagonally.
4+
5+
The following rules are applied to each cell:
6+
7+
- Any live cell with two or three live neighbors lives on.
8+
- Any dead cell with exactly three live neighbors becomes a live cell.
9+
- All other cells die or stay dead.
10+
11+
Given a matrix of 1s and 0s (corresponding to live and dead cells), apply the rules to each cell, and return the next generation.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Introduction
2+
3+
[Conway's Game of Life][game-of-life] is a fascinating cellular automaton created by the British mathematician John Horton Conway in 1970.
4+
5+
The game consists of a two-dimensional grid of cells that can either be "alive" or "dead."
6+
7+
After each generation, the cells interact with their eight neighbors via a set of rules, which define the new generation.
8+
9+
[game-of-life]: https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"keiravillekode"
4+
],
5+
"files": {
6+
"solution": [
7+
"game_of_life.s"
8+
],
9+
"test": [
10+
"game_of_life_test.c"
11+
],
12+
"example": [
13+
".meta/example.s"
14+
]
15+
},
16+
"blurb": "Implement Conway's Game of Life.",
17+
"source": "Wikipedia",
18+
"source_url": "https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life"
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
.text
2+
.globl tick
3+
4+
/* extern void tick(uint64_t *buffer, const uint64_t *matrix, size_t row_count, size_t column_count); */
5+
tick:
6+
stp x22, x23, [sp, #-48]! /* preserve registers */
7+
stp x24, x25, [sp, #16]
8+
stp x26, x27, [sp, #32]
9+
cbz x2, .return /* 0 rows? */
10+
11+
mov x5, xzr
12+
ldr x6, [x1], #8 /* read first row */
13+
14+
.outer:
15+
add x2, x2, #-1
16+
mov x4, x5 /* previous row */
17+
mov x5, x6 /* current row */
18+
mov x6, xzr
19+
cbz x2, .process_row
20+
21+
ldr x6, [x1], #8 /* read next row */
22+
23+
.process_row:
24+
mov x17, xzr
25+
mov x14, x4
26+
mov x15, x5
27+
mov x16, x6
28+
29+
and x23, x15, #1
30+
mov x25, xzr
31+
and x26, x16, #1
32+
add x26, x26, x23
33+
and x9, x14, #1
34+
add x26, x26, x9 /* total for first column across 3 rows */
35+
36+
mov x13, x3 /* number of columns remaining */
37+
cbz x3, .write
38+
39+
.inner:
40+
add x13, x13, #-1
41+
lsr x14, x14, #1 /* previous row, shifted */
42+
lsr x15, x15, #1 /* current row, shifted */
43+
lsr x16, x16, #1 /* next row, shifted */
44+
45+
mov x22, x23 /* current cell */
46+
and x23, x15, #1 /* next cell */
47+
mov x24, x25 /* total for previous column across 3 rows */
48+
mov x25, x26 /* total for current column across 3 rows */
49+
and x26, x16, #1
50+
add x26, x26, x23
51+
and x9, x14, #1
52+
add x26, x26, x9 /* total for next column across 3 rows */
53+
54+
add x27, x24, x25
55+
add x27, x27, x26
56+
sub x27, x27, x22 /* total of 8 neighbours */
57+
orr x27, x27, x22 /* if current cell is alive, treat 2 alive neighbours as 3 */
58+
cmp x27, #3
59+
cinc x17, x17, eq /* conditionally sets low bit */
60+
61+
ror x17, x17, #1 /* rotate right */
62+
cbnz x13, .inner /* remaining columns? */
63+
64+
.write:
65+
neg x9, x3
66+
rorv x17, x17, x9 /* rotate left by x3 */
67+
str x17, [x0], #8
68+
cbnz x2, .outer /* remaining rows? */
69+
70+
.return:
71+
ldp x26, x27, [sp, #32] /* restore registers */
72+
ldp x24, x25, [sp, #16]
73+
ldp x22, x23, [sp], #48
74+
ret
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
[ae86ea7d-bd07-4357-90b3-ac7d256bd5c5]
13+
description = "empty matrix"
14+
include = false
15+
16+
[4ea5ccb7-7b73-4281-954a-bed1b0f139a5]
17+
description = "live cells with zero live neighbors die"
18+
19+
[df245adc-14ff-4f9c-b2ae-f465ef5321b2]
20+
description = "live cells with only one live neighbor die"
21+
22+
[2a713b56-283c-48c8-adae-1d21306c80ae]
23+
description = "live cells with two live neighbors stay alive"
24+
25+
[86d5c5a5-ab7b-41a1-8907-c9b3fc5e9dae]
26+
description = "live cells with three live neighbors stay alive"
27+
28+
[015f60ac-39d8-4c6c-8328-57f334fc9f89]
29+
description = "dead cells with three live neighbors become alive"
30+
31+
[2ee69c00-9d41-4b8b-89da-5832e735ccf1]
32+
description = "live cells with four or more neighbors die"
33+
34+
[a79b42be-ed6c-4e27-9206-43da08697ef6]
35+
description = "bigger matrix"
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
ifeq ($(origin AS),default)
2+
AS = aarch64-linux-gnu-as
3+
endif
4+
ifeq ($(origin CC),default)
5+
CC = aarch64-linux-gnu-gcc
6+
endif
7+
8+
CFLAGS ?= -g -Wall -Wextra -pedantic -Werror
9+
LDFLAGS =
10+
11+
ALL_LDFLAGS = -pie -Wl,--fatal-warnings
12+
13+
ALL_CFLAGS = -std=c99 -fPIE $(CFLAGS)
14+
ALL_LDFLAGS += $(LDFLAGS)
15+
16+
C_OBJS = $(patsubst %.c,%.o,$(wildcard *.c))
17+
AS_OBJS = $(patsubst %.s,%.o,$(wildcard *.s))
18+
ALL_OBJS = $(filter-out example.o,$(C_OBJS) $(AS_OBJS) vendor/unity.o)
19+
20+
CC_CMD = $(CC) $(ALL_CFLAGS) -c -o $@ $<
21+
22+
PLATFORM = $(shell uname -m)
23+
ifeq ($(PLATFORM),aarch64)
24+
MAYBE_QEMU =
25+
else
26+
MAYBE_QEMU = qemu-aarch64 -L /usr/aarch64-linux-gnu
27+
endif
28+
29+
all: tests
30+
@$(MAYBE_QEMU) ./$<
31+
32+
tests: $(ALL_OBJS)
33+
@$(CC) $(ALL_CFLAGS) $(ALL_LDFLAGS) -o $@ $(ALL_OBJS)
34+
35+
%.o: %.s
36+
@$(AS) -o $@ $<
37+
38+
%.o: %.c
39+
@$(CC_CMD)
40+
41+
vendor/unity.o: vendor/unity.c vendor/unity.h vendor/unity_internals.h
42+
@$(CC_CMD)
43+
44+
clean:
45+
@rm -f *.o vendor/*.o tests
46+
47+
.PHONY: all clean
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.text
2+
.globl tick
3+
4+
tick:
5+
ret

0 commit comments

Comments
 (0)