Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add space-age exercise #140

Merged
merged 1 commit into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,14 @@
"prerequisites": [],
"difficulty": 5
},
{
"slug": "space-age",
"name": "Space Age",
"uuid": "1e130251-397b-4a76-baf0-5b6498900edc",
"practices": [],
"prerequisites": [],
"difficulty": 5
},
{
"slug": "knapsack",
"name": "Knapsack",
Expand Down
28 changes: 28 additions & 0 deletions exercises/practice/space-age/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Instructions

Given an age in seconds, calculate how old someone would be on a planet in our Solar System.

One Earth year equals 365.25 Earth days, or 31,557,600 seconds.
If you were told someone was 1,000,000,000 seconds old, their age would be 31.69 Earth-years.

For the other planets, you have to account for their orbital period in Earth Years:

| Planet | Orbital period in Earth Years |
| ------- | ----------------------------- |
| Mercury | 0.2408467 |
| Venus | 0.61519726 |
| Earth | 1.0 |
| Mars | 1.8808158 |
| Jupiter | 11.862615 |
| Saturn | 29.447498 |
| Uranus | 84.016846 |
| Neptune | 164.79132 |

~~~~exercism/note
The actual length of one complete orbit of the Earth around the sun is closer to 365.256 days (1 sidereal year).
The Gregorian calendar has, on average, 365.2425 days.
While not entirely accurate, 365.25 is the value used in this exercise.
See [Year on Wikipedia][year] for more ways to measure a year.

[year]: https://en.wikipedia.org/wiki/Year#Summary
~~~~
20 changes: 20 additions & 0 deletions exercises/practice/space-age/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Introduction

The year is 2525 and you've just embarked on a journey to visit all planets in the Solar System (Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus and Neptune).
The first stop is Mercury, where customs require you to fill out a form (bureaucracy is apparently _not_ Earth-specific).
As you hand over the form to the customs officer, they scrutinize it and frown.
"Do you _really_ expect me to believe you're just 50 years old?
You must be closer to 200 years old!"

Amused, you wait for the customs officer to start laughing, but they appear to be dead serious.
You realize that you've entered your age in _Earth years_, but the officer expected it in _Mercury years_!
As Mercury's orbital period around the sun is significantly shorter than Earth, you're actually a lot older in Mercury years.
After some quick calculations, you're able to provide your age in Mercury Years.
The customs officer smiles, satisfied, and waves you through.
You make a mental note to pre-calculate your planet-specific age _before_ future customs checks, to avoid such mix-ups.

~~~~exercism/note
If you're wondering why Pluto didn't make the cut, go watch [this YouTube video][pluto-video].

[pluto-video]: https://www.youtube.com/watch?v=Z_2gbGXzFbs
~~~~
19 changes: 19 additions & 0 deletions exercises/practice/space-age/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"keiravillekode"
],
"files": {
"solution": [
"space_age.s"
],
"test": [
"space_age_test.c"
],
"example": [
".meta/example.s"
]
},
"blurb": "Given an age in seconds, calculate how old someone is in terms of a given planet's solar years.",
"source": "Partially inspired by Chapter 1 in Chris Pine's online Learn to Program tutorial.",
"source_url": "https://pine.fm/LearnToProgram/?Chapter=01"
}
40 changes: 40 additions & 0 deletions exercises/practice/space-age/.meta/example.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
.equ MERCURY, 1
.equ VENUS, 2
.equ EARTH, 3
.equ MARS, 4
.equ JUPITER, 5
.equ SATURN, 6
.equ URANUS, 7
.equ NEPTUNE, 8

.section .data

year: .double 31557600.0
.double 0.2408467
.double 0.61519726
.double 1.0
.double 1.8808158
.double 11.862615
.double 29.447498
.double 84.016846
.double 164.79132

.text
.globl age

/* extern double age(planet_t planet, int seconds); */
age:
scvtf d0, w1

adrp x9, year
add x9, x9, :lo12:year

ldr d1, [x9]
fdiv d0, d0, d1

lsl w0, w0, #3
add x9, x9, x0
ldr d1, [x9]
fdiv d0, d0, d1

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

[84f609af-5a91-4d68-90a3-9e32d8a5cd34]
description = "age on Earth"

[ca20c4e9-6054-458c-9312-79679ffab40b]
description = "age on Mercury"

[502c6529-fd1b-41d3-8fab-65e03082b024]
description = "age on Venus"

[9ceadf5e-a0d5-4388-9d40-2c459227ceb8]
description = "age on Mars"

[42927dc3-fe5e-4f76-a5b5-f737fc19bcde]
description = "age on Jupiter"

[8469b332-7837-4ada-b27c-00ee043ebcad]
description = "age on Saturn"

[999354c1-76f8-4bb5-a672-f317b6436743]
description = "age on Uranus"

[80096d30-a0d4-4449-903e-a381178355d8]
description = "age on Neptune"

[57b96e2a-1178-40b7-b34d-f3c9c34e4bf4]
description = "invalid planet causes error"
include = false
36 changes: 36 additions & 0 deletions exercises/practice/space-age/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
14 changes: 14 additions & 0 deletions exercises/practice/space-age/space_age.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.equ MERCURY, 1
.equ VENUS, 2
.equ EARTH, 3
.equ MARS, 4
.equ JUPITER, 5
.equ SATURN, 6
.equ URANUS, 7
.equ NEPTUNE, 8

.text
.globl age

age:
ret
72 changes: 72 additions & 0 deletions exercises/practice/space-age/space_age_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include "vendor/unity.h"

typedef enum {
MERCURY = 1,
VENUS,
EARTH,
MARS,
JUPITER,
SATURN,
URANUS,
NEPTUNE
} planet_t;

extern double age(planet_t planet, int seconds);

void setUp(void) {
}

void tearDown(void) {
}

void test_age_on_earth(void) {
TEST_ASSERT_FLOAT_WITHIN(0.01, 31.69, age(EARTH, 1000000000));
}

void test_age_on_mercury(void) {
TEST_IGNORE();
TEST_ASSERT_FLOAT_WITHIN(0.01, 280.88, age(MERCURY, 2134835688));
}

void test_age_on_venus(void) {
TEST_IGNORE();
TEST_ASSERT_FLOAT_WITHIN(0.01, 9.78, age(VENUS, 189839836));
}

void test_age_on_mars(void) {
TEST_IGNORE();
TEST_ASSERT_FLOAT_WITHIN(0.01, 35.88, age(MARS, 2129871239));
}

void test_age_on_jupiter(void) {
TEST_IGNORE();
TEST_ASSERT_FLOAT_WITHIN(0.01, 2.41, age(JUPITER, 901876382));
}

void test_age_on_saturn(void) {
TEST_IGNORE();
TEST_ASSERT_FLOAT_WITHIN(0.01, 2.15, age(SATURN, 2000000000));
}

void test_age_on_uranus(void) {
TEST_IGNORE();
TEST_ASSERT_FLOAT_WITHIN(0.01, 0.46, age(URANUS, 1210123456));
}

void test_age_on_neptune(void) {
TEST_IGNORE();
TEST_ASSERT_FLOAT_WITHIN(0.01, 0.35, age(NEPTUNE, 1821023456));
}

int main(void) {
UNITY_BEGIN();
RUN_TEST(test_age_on_earth);
RUN_TEST(test_age_on_mercury);
RUN_TEST(test_age_on_venus);
RUN_TEST(test_age_on_mars);
RUN_TEST(test_age_on_jupiter);
RUN_TEST(test_age_on_saturn);
RUN_TEST(test_age_on_uranus);
RUN_TEST(test_age_on_neptune);
return UNITY_END();
}
Loading