diff options
author | H.J <thechairman@thechairman.info> | 2024-06-03 10:36:31 -0400 |
---|---|---|
committer | H.J <thechairman@thechairman.info> | 2024-06-03 10:36:31 -0400 |
commit | 7d704786ec158349f2f34590a858df6e78e844d6 (patch) | |
tree | 2a5b0f0af3bc2c322e941d25e2da9f9e66a6cdd3 /aoc2019-gleam | |
parent | 9f0484222417d6b1495636123ae7de278b9fd0e5 (diff) | |
download | gleam_aoc-7d704786ec158349f2f34590a858df6e78e844d6.tar.gz gleam_aoc-7d704786ec158349f2f34590a858df6e78e844d6.zip |
gleam day 1 2017
Diffstat (limited to 'aoc2019-gleam')
-rw-r--r-- | aoc2019-gleam/.github/workflows/test.yml | 23 | ||||
-rw-r--r-- | aoc2019-gleam/.gitignore | 4 | ||||
-rw-r--r-- | aoc2019-gleam/README.md | 25 | ||||
-rw-r--r-- | aoc2019-gleam/gleam.toml | 21 | ||||
-rw-r--r-- | aoc2019-gleam/manifest.toml | 33 | ||||
-rw-r--r-- | aoc2019-gleam/src/aoc2019_gleam.gleam | 5 | ||||
-rw-r--r-- | aoc2019-gleam/src/aoc_2019/day_1.gleam | 30 | ||||
-rw-r--r-- | aoc2019-gleam/src/aoc_2019/day_2.gleam | 81 | ||||
-rw-r--r-- | aoc2019-gleam/test/aoc2019_gleam_test.gleam | 12 |
9 files changed, 234 insertions, 0 deletions
diff --git a/aoc2019-gleam/.github/workflows/test.yml b/aoc2019-gleam/.github/workflows/test.yml new file mode 100644 index 0000000..dd5e246 --- /dev/null +++ b/aoc2019-gleam/.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@v4 + - uses: erlef/setup-beam@v1 + with: + otp-version: "26.0.2" + gleam-version: "1.2.0" + rebar3-version: "3" + # elixir-version: "1.15.4" + - run: gleam deps download + - run: gleam test + - run: gleam format --check src test diff --git a/aoc2019-gleam/.gitignore b/aoc2019-gleam/.gitignore new file mode 100644 index 0000000..599be4e --- /dev/null +++ b/aoc2019-gleam/.gitignore @@ -0,0 +1,4 @@ +*.beam +*.ez +/build +erl_crash.dump diff --git a/aoc2019-gleam/README.md b/aoc2019-gleam/README.md new file mode 100644 index 0000000..bbf8121 --- /dev/null +++ b/aoc2019-gleam/README.md @@ -0,0 +1,25 @@ +# aoc2019_gleam + +[](https://hex.pm/packages/aoc2019_gleam) +[](https://hexdocs.pm/aoc2019_gleam/) + +```sh +gleam add aoc2019_gleam +``` +```gleam +import aoc2019_gleam + +pub fn main() { + // TODO: An example of the project in use +} +``` + +Further documentation can be found at <https://hexdocs.pm/aoc2019_gleam>. + +## Development + +```sh +gleam run # Run the project +gleam test # Run the tests +gleam shell # Run an Erlang shell +``` diff --git a/aoc2019-gleam/gleam.toml b/aoc2019-gleam/gleam.toml new file mode 100644 index 0000000..c9ede3d --- /dev/null +++ b/aoc2019-gleam/gleam.toml @@ -0,0 +1,21 @@ +name = "aoc2019_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.0 and < 2.0.0" +gladvent = ">= 0.7.3 and < 1.0.0" +gary = ">= 1.0.1 and < 2.0.0" + +[dev-dependencies] +gleeunit = ">= 1.0.0 and < 2.0.0" diff --git a/aoc2019-gleam/manifest.toml b/aoc2019-gleam/manifest.toml new file mode 100644 index 0000000..12fa60f --- /dev/null +++ b/aoc2019-gleam/manifest.toml @@ -0,0 +1,33 @@ +# This file was generated by Gleam +# You typically do not need to edit this file + +packages = [ + { name = "argv", version = "1.0.2", build_tools = ["gleam"], requirements = [], otp_app = "argv", source = "hex", outer_checksum = "BA1FF0929525DEBA1CE67256E5ADF77A7CDDFE729E3E3F57A5BDCAA031DED09D" }, + { name = "filepath", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "EFB6FF65C98B2A16378ABC3EE2B14124168C0CE5201553DE652E2644DCFDB594" }, + { name = "gary", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gary", source = "hex", outer_checksum = "4C05611EEC74876A1E36309EED22C826B92F22E566579ACBC65C579CD615EC60" }, + { name = "gladvent", version = "0.7.3", build_tools = ["gleam"], requirements = ["argv", "filepath", "gleam_erlang", "gleam_json", "gleam_otp", "gleam_package_interface", "gleam_stdlib", "glint", "parallel_map", "shellout", "simplifile", "snag", "spinner", "tom"], otp_app = "gladvent", source = "hex", outer_checksum = "59D93FD759427BCE8EE9828C0B62A3CD18362225F948C9789D334DCEAD621358" }, + { name = "gleam_community_ansi", version = "1.4.0", build_tools = ["gleam"], requirements = ["gleam_community_colour", "gleam_stdlib"], otp_app = "gleam_community_ansi", source = "hex", outer_checksum = "FE79E08BF97009729259B6357EC058315B6FBB916FAD1C2FF9355115FEB0D3A4" }, + { name = "gleam_community_colour", version = "1.4.0", build_tools = ["gleam"], requirements = ["gleam_json", "gleam_stdlib"], otp_app = "gleam_community_colour", source = "hex", outer_checksum = "795964217EBEDB3DA656F5EB8F67D7AD22872EB95182042D3E7AFEF32D3FD2FE" }, + { name = "gleam_erlang", version = "0.25.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "054D571A7092D2A9727B3E5D183B7507DAB0DA41556EC9133606F09C15497373" }, + { name = "gleam_json", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "thoas"], otp_app = "gleam_json", source = "hex", outer_checksum = "9063D14D25406326C0255BDA0021541E797D8A7A12573D849462CAFED459F6EB" }, + { name = "gleam_otp", version = "0.10.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "0B04FE915ACECE539B317F9652CAADBBC0F000184D586AAAF2D94C100945D72B" }, + { name = "gleam_package_interface", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_json", "gleam_stdlib"], otp_app = "gleam_package_interface", source = "hex", outer_checksum = "52A721BCA972C8099BB881195D821AAA64B9F2655BECC102165D5A1097731F01" }, + { name = "gleam_stdlib", version = "0.38.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "663CF11861179AF415A625307447775C09404E752FF99A24E2057C835319F1BE" }, + { name = "glearray", version = "0.2.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "glearray", source = "hex", outer_checksum = "9C207E05F38D724F464FA921378DB3ABC2B0A2F5821116D8BC8B2CACC68930D5" }, + { name = "gleeunit", version = "1.1.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "72CDC3D3F719478F26C4E2C5FED3E657AC81EC14A47D2D2DEBB8693CA3220C3B" }, + { name = "glint", version = "1.0.0-rc2", build_tools = ["gleam"], requirements = ["gleam_community_ansi", "gleam_community_colour", "gleam_stdlib", "snag"], otp_app = "glint", source = "hex", outer_checksum = "FD5C47CE237CA67121F3946ADE7C630750BB67F5E8A4717D2DF5B5EE758CCFDB" }, + { name = "parallel_map", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_otp", "gleam_stdlib"], otp_app = "parallel_map", source = "hex", outer_checksum = "653714A9FD63EACD1A9D0A6582A972B0EC109AE275CDDD2E99CFC3DFAFAB9225" }, + { name = "repeatedly", version = "2.1.1", build_tools = ["gleam"], requirements = [], otp_app = "repeatedly", source = "hex", outer_checksum = "38808C3EC382B0CD981336D5879C24ECB37FCB9C1D1BD128F7A80B0F74404D79" }, + { name = "shellout", version = "1.6.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "shellout", source = "hex", outer_checksum = "E2FCD18957F0E9F67E1F497FC9FF57393392F8A9BAEAEA4779541DE7A68DD7E0" }, + { name = "simplifile", version = "1.7.0", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "1D5DFA3A2F9319EC85825F6ED88B8E449F381B0D55A62F5E61424E748E7DDEB0" }, + { name = "snag", version = "0.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "snag", source = "hex", outer_checksum = "54D32E16E33655346AA3E66CBA7E191DE0A8793D2C05284E3EFB90AD2CE92BCC" }, + { name = "spinner", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_community_ansi", "gleam_erlang", "gleam_stdlib", "glearray", "repeatedly"], otp_app = "spinner", source = "hex", outer_checksum = "200BA3D4A04D468898E63C0D316E23F526E02514BC46454091975CB5BAE41E8F" }, + { name = "thoas", version = "1.2.1", build_tools = ["rebar3"], requirements = [], otp_app = "thoas", source = "hex", outer_checksum = "E38697EDFFD6E91BD12CEA41B155115282630075C2A727E7A6B2947F5408B86A" }, + { name = "tom", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "tom", source = "hex", outer_checksum = "A5364613E3DBF77F38EFF81DA9F99324086D029EC2B2D44348762FBE38602311" }, +] + +[requirements] +gary = { version = ">= 1.0.1 and < 2.0.0"} +gladvent = { version = ">= 0.7.3 and < 1.0.0" } +gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" } +gleeunit = { version = ">= 1.0.0 and < 2.0.0" } diff --git a/aoc2019-gleam/src/aoc2019_gleam.gleam b/aoc2019-gleam/src/aoc2019_gleam.gleam new file mode 100644 index 0000000..c5a7e0a --- /dev/null +++ b/aoc2019-gleam/src/aoc2019_gleam.gleam @@ -0,0 +1,5 @@ +import gladvent + +pub fn main() { + gladvent.main() +} diff --git a/aoc2019-gleam/src/aoc_2019/day_1.gleam b/aoc2019-gleam/src/aoc_2019/day_1.gleam new file mode 100644 index 0000000..8a7fd2d --- /dev/null +++ b/aoc2019-gleam/src/aoc_2019/day_1.gleam @@ -0,0 +1,30 @@ +import gleam/int +import gleam/list +import gleam/result +import gleam/string + +pub fn parse(input: String) -> List(Int) { + input + |> string.split("\n") + |> list.map(int.parse) + |> result.values() +} + +pub fn pt_1(input: List(Int)) { + list.fold(input, 0, fn(total, next) { total + naive_fuel(next) }) +} + +pub fn pt_2(input: List(Int)) { + list.fold(input, 0, fn(total, next) { total + recursive_fuel(next) }) +} + +fn naive_fuel(weight: Int) -> Int { + { weight / 3 } - 2 +} + +fn recursive_fuel(weight: Int) -> Int { + case { weight / 3 } - 2 { + n if n <= 0 -> 0 + n -> n + recursive_fuel(n) + } +} diff --git a/aoc2019-gleam/src/aoc_2019/day_2.gleam b/aoc2019-gleam/src/aoc_2019/day_2.gleam new file mode 100644 index 0000000..8faa0ea --- /dev/null +++ b/aoc2019-gleam/src/aoc_2019/day_2.gleam @@ -0,0 +1,81 @@ +import gary.{type ErlangArray} +import gary/array.{type ArrayError} +import gleam/bool +import gleam/int +import gleam/list +import gleam/result +import gleam/string + +pub fn parse(input: String) -> ErlangArray(Int) { + input + |> string.split(",") + |> list.map(int.parse) + |> result.values() + |> array.from_list(default: -1) + |> array.make_fixed() +} + +pub fn pt_1(input: ErlangArray(Int)) -> Int { + let assert Ok(result) = + input + |> edit_starting_intcodes(12, 2) + |> run_intcode(0) + + result +} + +pub fn pt_2(input: ErlangArray(Int)) -> Int { + let assert [result] = { + use noun <- list.flat_map(list.range(0, 99)) + use verb <- list.filter_map(list.range(0, 99)) + let result = input |> edit_starting_intcodes(noun, verb) |> run_intcode(0) + case result == Ok(19_690_720) { + True -> Ok(100 * noun + verb) + False -> Error(Nil) + } + } + + result +} + +fn run_intcode( + intcode: ErlangArray(Int), + pointer: Int, +) -> Result(Int, ArrayError) { + let assert Ok(op_code) = array.get(intcode, pointer) + let op = get_op(op_code) + + use <- bool.guard(result.is_error(op), array.get(intcode, 0)) + let assert Ok(position_1) = array.get(intcode, pointer + 1) + let assert Ok(position_2) = array.get(intcode, pointer + 2) + let assert Ok(position_3) = array.get(intcode, pointer + 3) + + let assert Ok(value_1) = array.get(intcode, position_1) + let assert Ok(value_2) = array.get(intcode, position_2) + + let assert Ok(f) = op + let new_value = f(value_1, value_2) + let assert Ok(updated_intcode) = array.set(intcode, position_3, new_value) + run_intcode(updated_intcode, pointer + 4) +} + +fn edit_starting_intcodes( + intcodes: ErlangArray(Int), + new_code_1: Int, + new_code_2: Int, +) -> ErlangArray(Int) { + let assert Ok(updated) = + intcodes + |> array.set(at: 1, put: new_code_1) + |> result.try(array.set(into: _, at: 2, put: new_code_2)) + updated +} + +fn get_op(code: Int) -> Result(fn(Int, Int) -> Int, Nil) { + case code { + 1 -> Ok(int.add) + 2 -> Ok(int.multiply) + 99 -> Error(Nil) + _ -> panic as "bad opcode" + } +} diff --git a/aoc2019-gleam/test/aoc2019_gleam_test.gleam b/aoc2019-gleam/test/aoc2019_gleam_test.gleam new file mode 100644 index 0000000..3831e7a --- /dev/null +++ b/aoc2019-gleam/test/aoc2019_gleam_test.gleam @@ -0,0 +1,12 @@ +import gleeunit +import gleeunit/should + +pub fn main() { + gleeunit.main() +} + +// gleeunit test functions end in `_test` +pub fn hello_world_test() { + 1 + |> should.equal(1) +} |