diff --git a/zig/grains/.exercism/config.json b/zig/grains/.exercism/config.json new file mode 100644 index 0000000..1d9768e --- /dev/null +++ b/zig/grains/.exercism/config.json @@ -0,0 +1,22 @@ +{ + "authors": [ + "massivelivefun" + ], + "contributors": [ + "ee7" + ], + "files": { + "solution": [ + "grains.zig" + ], + "test": [ + "test_grains.zig" + ], + "example": [ + ".meta/example.zig" + ] + }, + "blurb": "Calculate the number of grains of wheat on a chessboard given that the number on each square doubles.", + "source": "The CodeRanch Cattle Drive, Assignment 6", + "source_url": "https://coderanch.com/wiki/718824/Grains" +} diff --git a/zig/grains/.exercism/metadata.json b/zig/grains/.exercism/metadata.json new file mode 100644 index 0000000..4aaf2e6 --- /dev/null +++ b/zig/grains/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"zig","exercise":"grains","id":"04039904d0414848b219497a9a412373","url":"https://exercism.org/tracks/zig/exercises/grains","handle":"Chomp1295","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/zig/grains/HELP.md b/zig/grains/HELP.md new file mode 100644 index 0000000..2c27883 --- /dev/null +++ b/zig/grains/HELP.md @@ -0,0 +1,53 @@ +# Help + +## Running the tests + +Write your code in `.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 grains.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) +- The [Zig track's programming category on the forum](https://forum.exercism.org/c/programming/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 Guide][zig-guide] is an excellent primer that explains the language features that Zig has to offer. +- [Ziglings][ziglings] is highly recommended. + Learn Zig by fixing tiny broken programs. +- [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-guide]: https://zig.guide/ +[ziglings]: https://codeberg.org/ziglings/exercises \ No newline at end of file diff --git a/zig/grains/README.md b/zig/grains/README.md new file mode 100644 index 0000000..4c6288b --- /dev/null +++ b/zig/grains/README.md @@ -0,0 +1,34 @@ +# Grains + +Welcome to Grains on Exercism's Zig Track. +If you need help running the tests or submitting your code, check out `HELP.md`. + +## Instructions + +Calculate the number of grains of wheat on a chessboard given that the number on each square doubles. + +There once was a wise servant who saved the life of a prince. +The king promised to pay whatever the servant could dream up. +Knowing that the king loved chess, the servant told the king he would like to have grains of wheat. +One grain on the first square of a chess board, with the number of grains doubling on each successive square. + +There are 64 squares on a chessboard (where square 1 has one grain, square 2 has two grains, and so on). + +Write code that shows: + +- how many grains were on a given square, and +- the total number of grains on the chessboard + +## Source + +### Created by + +- @massivelivefun + +### Contributed to by + +- @ee7 + +### Based on + +The CodeRanch Cattle Drive, Assignment 6 - https://coderanch.com/wiki/718824/Grains \ No newline at end of file diff --git a/zig/grains/grains.zig b/zig/grains/grains.zig new file mode 100644 index 0000000..acd01fe --- /dev/null +++ b/zig/grains/grains.zig @@ -0,0 +1,13 @@ +const std = @import("std"); + +pub const ChessboardError = error{IndexOutOfBounds}; + +pub fn square(index: usize) ChessboardError!u64 { + if (index > 64 or index < 1) return error.IndexOutOfBounds; + + return std.math.pow(u64, 2, index - 1); +} + +pub fn total() u64 { + return std.math.maxInt(u64); +} diff --git a/zig/grains/test_grains.zig b/zig/grains/test_grains.zig new file mode 100644 index 0000000..37f2df6 --- /dev/null +++ b/zig/grains/test_grains.zig @@ -0,0 +1,65 @@ +const std = @import("std"); +const testing = std.testing; + +const grains = @import("grains.zig"); +const ChessboardError = grains.ChessboardError; + +test "grains on square 1" { + const expected: u64 = 1; + const actual = try grains.square(1); + try testing.expectEqual(expected, actual); +} + +test "grains on square 2" { + const expected: u64 = 2; + const actual = try grains.square(2); + try testing.expectEqual(expected, actual); +} + +test "grains on square 3" { + const expected: u64 = 4; + const actual = try grains.square(3); + try testing.expectEqual(expected, actual); +} + +test "grains on square 4" { + const expected: u64 = 8; + const actual = try grains.square(4); + try testing.expectEqual(expected, actual); +} + +test "grains on square 16" { + const expected: u64 = 32_768; + const actual = try grains.square(16); + try testing.expectEqual(expected, actual); +} + +test "grains on square 32" { + const expected: u64 = 2_147_483_648; + const actual = try grains.square(32); + try testing.expectEqual(expected, actual); +} + +test "grains on square 64" { + const expected: u64 = 9_223_372_036_854_775_808; + const actual = try grains.square(64); + try testing.expectEqual(expected, actual); +} + +test "square 0 produces an error" { + const expected = ChessboardError.IndexOutOfBounds; + const actual = grains.square(0); + try testing.expectError(expected, actual); +} + +test "square greater than 64 produces an error" { + const expected = ChessboardError.IndexOutOfBounds; + const actual = grains.square(65); + try testing.expectError(expected, actual); +} + +test "returns the total number of grains on the board" { + const expected: u64 = 18_446_744_073_709_551_615; + const actual = grains.total(); + try testing.expectEqual(expected, actual); +}