diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..951b2fe --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,23 @@ +name: test + +on: + push: + branches: + - master + - main + pull_request: + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: erlef/setup-beam@v1 + with: + otp-version: "26.0.2" + gleam-version: "0.34.1" + rebar3-version: "3" + # elixir-version: "1.15.4" + - run: gleam deps download + - run: gleam test + - run: gleam format --check src test diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..599be4e --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.beam +*.ez +/build +erl_crash.dump diff --git a/README.md b/README.md new file mode 100644 index 0000000..8359e8e --- /dev/null +++ b/README.md @@ -0,0 +1,25 @@ +# ak_day_play_gleam + +[![Package Version](https://img.shields.io/hexpm/v/ak_day_play_gleam)](https://hex.pm/packages/ak_day_play_gleam) +[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/ak_day_play_gleam/) + +```sh +gleam add ak_day_play_gleam +``` +```gleam +import ak_day_play_gleam + +pub fn main() { + // TODO: An example of the project in use +} +``` + +Further documentation can be found at . + +## Development + +```sh +gleam run # Run the project +gleam test # Run the tests +gleam shell # Run an Erlang shell +``` diff --git a/gleam.toml b/gleam.toml new file mode 100644 index 0000000..d9dae3e --- /dev/null +++ b/gleam.toml @@ -0,0 +1,19 @@ +name = "ak_day_play_gleam" +version = "1.0.0" + +# Fill out these fields if you intend to generate HTML documentation or publish +# your project to the Hex package manager. +# +# description = "" +# licences = ["Apache-2.0"] +# repository = { type = "github", user = "username", repo = "project" } +# links = [{ title = "Website", href = "https://gleam.run" }] +# +# For a full reference of all the available options, you can have a look at +# https://gleam.run/writing-gleam/gleam-toml/. + +[dependencies] +gleam_stdlib = "~> 0.34 or ~> 1.0" + +[dev-dependencies] +gleeunit = "~> 1.0" diff --git a/manifest.toml b/manifest.toml new file mode 100644 index 0000000..1ee80fb --- /dev/null +++ b/manifest.toml @@ -0,0 +1,11 @@ +# This file was generated by Gleam +# You typically do not need to edit this file + +packages = [ + { name = "gleam_stdlib", version = "0.35.1", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "5443EEB74708454B65650FEBBB1EF5175057D1DEC62AEA9D7C6D96F41DA79152" }, + { name = "gleeunit", version = "1.0.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "D364C87AFEB26BDB4FB8A5ABDE67D635DC9FA52D6AB68416044C35B096C6882D" }, +] + +[requirements] +gleam_stdlib = { version = "~> 0.34 or ~> 1.0" } +gleeunit = { version = "~> 1.0" } diff --git a/src/ak_day_play_gleam.gleam b/src/ak_day_play_gleam.gleam new file mode 100644 index 0000000..371b767 --- /dev/null +++ b/src/ak_day_play_gleam.gleam @@ -0,0 +1,24 @@ +import gleam/io +import gleam/int +import gleam/result +import gleam/list + +pub fn main() { + io.println("Hello from ak_day_play_gleam!") +} + +pub fn score(rolls) { + let frames = list.range(0, 9) + int.sum(list.map(frames, fn(x) { frame_score(x, rolls) })) +} + +fn frame_score(frame_number, rolls) { + let initial_index = 2 * frame_number + case + result.unwrap(list.at(rolls, initial_index), 0) + + result.unwrap(list.at(rolls, initial_index + 1), 0) + { + 10 -> 10 + result.unwrap(list.at(rolls, initial_index + 2), 0) + value -> value + } +} diff --git a/test/ak_day_play_gleam_test.gleam b/test/ak_day_play_gleam_test.gleam new file mode 100644 index 0000000..26d092b --- /dev/null +++ b/test/ak_day_play_gleam_test.gleam @@ -0,0 +1,71 @@ +import gleeunit +import gleeunit/should +import ak_day_play_gleam + +pub fn main() { + gleeunit.main() +} + +// gleeunit test functions end in `_test` +pub fn gutter_ball_game_test() { + ak_day_play_gleam.score([ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + ]) + |> should.equal(0) +} + +pub fn score_of_1_test() { + ak_day_play_gleam.score([ + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + ]) + |> should.equal(1) +} + +pub fn score_of_1_not_in_first_frame_test() { + ak_day_play_gleam.score([ + 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + ]) + |> should.equal(1) +} + +pub fn score_of_2_with_two_singles_test() { + ak_day_play_gleam.score([ + 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + ]) + |> should.equal(2) +} + +pub fn score_of_3_with_two_non_zero_rolls_test() { + ak_day_play_gleam.score([ + 0, 0, 2, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + ]) + |> should.equal(3) +} + +pub fn score_spare_test() { + ak_day_play_gleam.score([ + 6, 4, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + ]) + |> should.equal(12) +} + +pub fn score_spare_test_doubling_two_test() { + ak_day_play_gleam.score([ + 6, 4, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + ]) + |> should.equal(14) +} + +pub fn score_strike_doubles_the_next_frame_test() { + ak_day_play_gleam.score([ + 10, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + ]) + |> should.equal(14) +} + +pub fn score_adds_up_to_eleven_but_no_spare_test() { + ak_day_play_gleam.score([ + 6, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, + ]) + |> should.equal(11) +}