aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJ.J <thechairman@thechairman.info>2024-05-30 23:01:43 -0400
committerJ.J <thechairman@thechairman.info>2024-05-30 23:01:43 -0400
commit7c63a6e1513f13a5d2e5253e66ca77cdc0827950 (patch)
treec56e8ce2f27c93edfdf4642f0c75f15b2b5ed1ab
parent2f6e8ac780bf11c420d0f606efd2d68ba3d79fa0 (diff)
downloadgleam_aoc-7c63a6e1513f13a5d2e5253e66ca77cdc0827950.tar.gz
gleam_aoc-7c63a6e1513f13a5d2e5253e66ca77cdc0827950.zip
gleam 2019 day 2
-rw-r--r--aoc2019_gleam/gleam.toml1
-rw-r--r--aoc2019_gleam/manifest.toml4
-rw-r--r--aoc2019_gleam/src/aoc_2019/day_2.gleam81
3 files changed, 85 insertions, 1 deletions
diff --git a/aoc2019_gleam/gleam.toml b/aoc2019_gleam/gleam.toml
index c9bed2a..c9ede3d 100644
--- a/aoc2019_gleam/gleam.toml
+++ b/aoc2019_gleam/gleam.toml
@@ -15,6 +15,7 @@ version = "1.0.0"
[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
index d41f27a..12fa60f 100644
--- a/aoc2019_gleam/manifest.toml
+++ b/aoc2019_gleam/manifest.toml
@@ -4,6 +4,7 @@
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" },
@@ -26,6 +27,7 @@ packages = [
]
[requirements]
-gladvent = { version = ">= 0.7.3 and < 1.0.0"}
+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/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"
+ }
+}