Skip to content

Commit

Permalink
Zig track WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
theodesp committed Mar 16, 2023
1 parent ea57ed3 commit da22555
Show file tree
Hide file tree
Showing 20 changed files with 676 additions and 3 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,7 @@ Tracks List
- [x] - isogram
- [x] - hamming
- [x] - grains
- [x] - resistor color
- [x] - resistor color
- [x] - resistor color duo
- [x] - triangle
- [x] - darts
18 changes: 18 additions & 0 deletions zig/zig/darts/.exercism/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"authors": [
"massivelivefun"
],
"files": {
"solution": [
"darts.zig"
],
"test": [
"test_darts.zig"
],
"example": [
".meta/example.zig"
]
},
"blurb": "Write a function that returns the earned points in a single toss of a Darts game.",
"source": "Inspired by an exercise created by a professor Della Paolera in Argentina"
}
1 change: 1 addition & 0 deletions zig/zig/darts/.exercism/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"track":"zig","exercise":"darts","id":"c7f4946c571847fbb0f2b6d926bf55f0","url":"https://exercism.org/tracks/zig/exercises/darts","handle":"theodesp","is_requester":true,"auto_approve":false}
49 changes: 49 additions & 0 deletions zig/zig/darts/HELP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Help

## Running the tests

Write your code in `<exercise_name>.zig`.

To run the tests for an exercise, run:

```bash
zig test test_exercise_name.zig
```

in the exercise's root directory (replacing `exercise_name` with the name of the exercise).

## Submitting your solution

You can submit your solution using the `exercism submit darts.zig` command.
This command will upload your solution to the Exercism website and print the solution page's URL.

It's possible to submit an incomplete solution which allows you to:

- See how others have completed the exercise
- Request help from a mentor

## Need to get help?

If you'd like help solving the exercise, check the following pages:

- The [Zig track's documentation](https://exercism.org/docs/tracks/zig)
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)

Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.

- [The Zig Programming Language Documentation][documentation] is a great overview of all of the language features that Zig provides to those who use it.
- [Zig Learn][zig-learn] is an excellent primer that explains the language features that Zig has to offer.
- [The Zig Programming Language Discord][discord-zig] is the main [Discord][discord].
It provides a great way to get in touch with the Zig community at large, and get some quick, direct help for any Zig related problem.
- [#zig][irc] on irc.freenode.net is the main Zig IRC channel.
- [/r/Zig][reddit] is the main Zig subreddit.
- [Stack Overflow][stack-overflow] can be used to discover code snippets and solutions to problems that may have already asked and maybe solved by others.

[discord]: https://discordapp.com
[discord-zig]: https://discord.com/invite/gxsFFjE
[documentation]: https://ziglang.org/documentation/master
[irc]: https://webchat.freenode.net/?channels=%23zig
[reddit]: https://www.reddit.com/r/Zig
[stack-overflow]: https://stackoverflow.com/questions/tagged/zig
[zig-learn]: https://ziglearn.org/
38 changes: 38 additions & 0 deletions zig/zig/darts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Darts

Welcome to Darts on Exercism's Zig Track.
If you need help running the tests or submitting your code, check out `HELP.md`.

## Instructions

Write a function that returns the earned points in a single toss of a Darts game.

[Darts][darts] is a game where players throw darts at a [target][darts-target].

In our particular instance of the game, the target rewards 4 different amounts of points, depending on where the dart lands:

- If the dart lands outside the target, player earns no points (0 points).
- If the dart lands in the outer circle of the target, player earns 1 point.
- If the dart lands in the middle circle of the target, player earns 5 points.
- If the dart lands in the inner circle of the target, player earns 10 points.

The outer circle has a radius of 10 units (this is equivalent to the total radius for the entire target), the middle circle a radius of 5 units, and the inner circle a radius of 1.
Of course, they are all centered at the same point (that is, the circles are [concentric][] defined by the coordinates (0, 0).

Write a function that given a point in the target (defined by its [Cartesian coordinates][cartesian-coordinates] `x` and `y`, where `x` and `y` are [real][real-numbers]), returns the correct amount earned by a dart landing at that point.

[darts]: https://en.wikipedia.org/wiki/Darts
[darts-target]: https://en.wikipedia.org/wiki/Darts#/media/File:Darts_in_a_dartboard.jpg
[concentric]: https://mathworld.wolfram.com/ConcentricCircles.html
[cartesian-coordinates]: https://www.mathsisfun.com/data/cartesian-coordinates.html
[real-numbers]: https://www.mathsisfun.com/numbers/real-numbers.html

## Source

### Created by

- @massivelivefun

### Based on

Inspired by an exercise created by a professor Della Paolera in Argentina
23 changes: 23 additions & 0 deletions zig/zig/darts/darts.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
pub const Coordinate = struct {
x: f32, y: f32,

pub fn init(x_coord: f32, y_coord: f32) Coordinate {
return Coordinate{.x = x_coord, .y = y_coord};
}
pub fn score(self: Coordinate) usize {
if (isInsideCircle(self.x, self.y, 0, 0, 1)) {
return 10;
}
if (isInsideCircle(self.x, self.y, 0, 0, 5)) {
return 5;
}
if (isInsideCircle(self.x, self.y, 0, 0, 10)) {
return 1;
}
return 0;
}
};

fn isInsideCircle(x: f32, y: f32, c_x: f32, c_y: f32, r: f32) bool {
return r * r >= (x-c_x) * (x-c_x) + (y-c_y) * (y-c_y);
}
95 changes: 95 additions & 0 deletions zig/zig/darts/test_darts.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
const std = @import("std");
const testing = std.testing;

const darts = @import("darts.zig");

test "missed target" {
const expected: usize = 0;
const coordinate = darts.Coordinate.init(-9.0, 9.0);
const actual = coordinate.score();
try testing.expectEqual(expected, actual);
}

test "on the outer circle" {
const expected: usize = 1;
const coordinate = darts.Coordinate.init(0.0, 10.0);
const actual = coordinate.score();
try testing.expectEqual(expected, actual);
}

test "on the middle circle" {
const expected: usize = 5;
const coordinate = darts.Coordinate.init(-5.0, 0.0);
const actual = coordinate.score();
try testing.expectEqual(expected, actual);
}

test "on the inner circle" {
const expected: usize = 10;
const coordinate = darts.Coordinate.init(0.0, -1.0);
const actual = coordinate.score();
try testing.expectEqual(expected, actual);
}

test "exactly on center" {
const expected: usize = 10;
const coordinate = darts.Coordinate.init(0.0, 0.0);
const actual = coordinate.score();
try testing.expectEqual(expected, actual);
}

test "near the center" {
const expected: usize = 10;
const coordinate = darts.Coordinate.init(-0.1, -0.1);
const actual = coordinate.score();
try testing.expectEqual(expected, actual);
}

test "just within the inner circle" {
const expected: usize = 10;
const coordinate = darts.Coordinate.init(0.7, 0.7);
const actual = coordinate.score();
try testing.expectEqual(expected, actual);
}

test "just outside the inner circle" {
const expected: usize = 5;
const coordinate = darts.Coordinate.init(0.8, -0.8);
const actual = coordinate.score();
try testing.expectEqual(expected, actual);
}

test "just within the middle circle" {
const expected: usize = 5;
const coordinate = darts.Coordinate.init(3.5, -3.5);
const actual = coordinate.score();
try testing.expectEqual(expected, actual);
}

test "just outside the middle circle" {
const expected: usize = 1;
const coordinate = darts.Coordinate.init(-3.6, -3.6);
const actual = coordinate.score();
try testing.expectEqual(expected, actual);
}

test "just within the outer circle" {
const expected: usize = 1;
const coordinate = darts.Coordinate.init(-7.0, 7.0);
const actual = coordinate.score();
try testing.expectEqual(expected, actual);
}

test "just outside the outer circle" {
const expected: usize = 0;
const coordinate = darts.Coordinate.init(7.1, -7.1);
const actual = coordinate.score();
try testing.expectEqual(expected, actual);
}

test "asymmetric position between the inner and middle circles" {
const expected: usize = 5;
const coordinate = darts.Coordinate.init(0.5, -4.0);
const actual = coordinate.score();
try testing.expectEqual(expected, actual);
}
19 changes: 19 additions & 0 deletions zig/zig/resistor-color-duo/.exercism/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"massivelivefun"
],
"files": {
"solution": [
"resistor_color_duo.zig"
],
"test": [
"test_resistor_color_duo.zig"
],
"example": [
".meta/example.zig"
]
},
"blurb": "Convert color codes, as used on resistors, to a numeric value.",
"source": "Maud de Vries, Erik Schierboom",
"source_url": "https://github.com/exercism/problem-specifications/issues/1464"
}
1 change: 1 addition & 0 deletions zig/zig/resistor-color-duo/.exercism/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"track":"zig","exercise":"resistor-color-duo","id":"4026a021f4bd42069f154709027f234e","url":"https://exercism.org/tracks/zig/exercises/resistor-color-duo","handle":"theodesp","is_requester":true,"auto_approve":false}
49 changes: 49 additions & 0 deletions zig/zig/resistor-color-duo/HELP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Help

## Running the tests

Write your code in `<exercise_name>.zig`.

To run the tests for an exercise, run:

```bash
zig test test_exercise_name.zig
```

in the exercise's root directory (replacing `exercise_name` with the name of the exercise).

## Submitting your solution

You can submit your solution using the `exercism submit resistor_color_duo.zig` command.
This command will upload your solution to the Exercism website and print the solution page's URL.

It's possible to submit an incomplete solution which allows you to:

- See how others have completed the exercise
- Request help from a mentor

## Need to get help?

If you'd like help solving the exercise, check the following pages:

- The [Zig track's documentation](https://exercism.org/docs/tracks/zig)
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)

Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.

- [The Zig Programming Language Documentation][documentation] is a great overview of all of the language features that Zig provides to those who use it.
- [Zig Learn][zig-learn] is an excellent primer that explains the language features that Zig has to offer.
- [The Zig Programming Language Discord][discord-zig] is the main [Discord][discord].
It provides a great way to get in touch with the Zig community at large, and get some quick, direct help for any Zig related problem.
- [#zig][irc] on irc.freenode.net is the main Zig IRC channel.
- [/r/Zig][reddit] is the main Zig subreddit.
- [Stack Overflow][stack-overflow] can be used to discover code snippets and solutions to problems that may have already asked and maybe solved by others.

[discord]: https://discordapp.com
[discord-zig]: https://discord.com/invite/gxsFFjE
[documentation]: https://ziglang.org/documentation/master
[irc]: https://webchat.freenode.net/?channels=%23zig
[reddit]: https://www.reddit.com/r/Zig
[stack-overflow]: https://stackoverflow.com/questions/tagged/zig
[zig-learn]: https://ziglearn.org/
48 changes: 48 additions & 0 deletions zig/zig/resistor-color-duo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Resistor Color Duo

Welcome to Resistor Color Duo on Exercism's Zig Track.
If you need help running the tests or submitting your code, check out `HELP.md`.

## Instructions

If you want to build something using a Raspberry Pi, you'll probably use _resistors_.
For this exercise, you need to know two things about them:

- Each resistor has a resistance value.
- Resistors are small - so small in fact that if you printed the resistance value on them, it would be hard to read.

To get around this problem, manufacturers print color-coded bands onto the resistors to denote their resistance values.
Each band has a position and a numeric value.

The first 2 bands of a resistor have a simple encoding scheme: each color maps to a single number.
For example, if they printed a brown band (value 1) followed by a green band (value 5), it would translate to the number 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.
The program will take color names as input and output a two digit number, even if the input is more than two colors!

The band colors are encoded as follows:

- Black: 0
- Brown: 1
- Red: 2
- Orange: 3
- Yellow: 4
- Green: 5
- Blue: 6
- Violet: 7
- Grey: 8
- White: 9

From the example above:
brown-green should return 15
brown-green-violet should return 15 too, ignoring the third color.

## Source

### Created by

- @massivelivefun

### Based on

Maud de Vries, Erik Schierboom - https://github.com/exercism/problem-specifications/issues/1464
19 changes: 19 additions & 0 deletions zig/zig/resistor-color-duo/resistor_color_duo.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
pub const ColorBand = enum(u8) {
black,
brown,
red,
orange,
yellow,
green,
blue,
violet,
grey,
white,
};

pub fn colorCode(colors: [2]ColorBand) usize {
if (colors[0] == ColorBand.black) {
return @enumToInt(colors[1]);
}
return @enumToInt(colors[0]) * 10 + @enumToInt(colors[1]);
}
Loading

0 comments on commit da22555

Please sign in to comment.