aboutsummaryrefslogtreecommitdiff
path: root/aoc2019_gleam/src
diff options
context:
space:
mode:
authorH.J <thechairman@thechairman.info>2024-06-03 10:36:31 -0400
committerH.J <thechairman@thechairman.info>2024-06-03 10:36:31 -0400
commit7d704786ec158349f2f34590a858df6e78e844d6 (patch)
tree2a5b0f0af3bc2c322e941d25e2da9f9e66a6cdd3 /aoc2019_gleam/src
parent9f0484222417d6b1495636123ae7de278b9fd0e5 (diff)
downloadgleam_aoc-7d704786ec158349f2f34590a858df6e78e844d6.tar.gz
gleam_aoc-7d704786ec158349f2f34590a858df6e78e844d6.zip
gleam day 1 2017
Diffstat (limited to 'aoc2019_gleam/src')
-rw-r--r--aoc2019_gleam/src/aoc2019_gleam.gleam5
-rw-r--r--aoc2019_gleam/src/aoc_2019/day_1.gleam30
-rw-r--r--aoc2019_gleam/src/aoc_2019/day_2.gleam81
3 files changed, 0 insertions, 116 deletions
diff --git a/aoc2019_gleam/src/aoc2019_gleam.gleam b/aoc2019_gleam/src/aoc2019_gleam.gleam
deleted file mode 100644
index c5a7e0a..0000000
--- a/aoc2019_gleam/src/aoc2019_gleam.gleam
+++ /dev/null
@@ -1,5 +0,0 @@
-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
deleted file mode 100644
index 8a7fd2d..0000000
--- a/aoc2019_gleam/src/aoc_2019/day_1.gleam
+++ /dev/null
@@ -1,30 +0,0 @@
-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
deleted file mode 100644
index 8faa0ea..0000000
--- a/aoc2019_gleam/src/aoc_2019/day_2.gleam
+++ /dev/null
@@ -1,81 +0,0 @@
-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"
- }
-}