diff options
author | H.J <thechairman@thechairman.info> | 2024-06-17 17:41:37 -0400 |
---|---|---|
committer | H.J <thechairman@thechairman.info> | 2024-06-17 17:41:37 -0400 |
commit | ef2ad0ee020b6754c230ae08f5979948b8db1350 (patch) | |
tree | 8d0397e5a4154c524412526a6b6d341dfee24c5a | |
parent | c0b58af106690cadd1b58bf84481748a717b4dfc (diff) | |
download | gleam_aoc-ef2ad0ee020b6754c230ae08f5979948b8db1350.tar.gz gleam_aoc-ef2ad0ee020b6754c230ae08f5979948b8db1350.zip |
gleam 2017 day 17 complete
-rw-r--r-- | aoc2017-gleam/gleam.toml | 1 | ||||
-rw-r--r-- | aoc2017-gleam/manifest.toml | 3 | ||||
-rw-r--r-- | aoc2017-gleam/src/aoc_2017/day_17.gleam | 55 |
3 files changed, 58 insertions, 1 deletions
diff --git a/aoc2017-gleam/gleam.toml b/aoc2017-gleam/gleam.toml index b1de573..e00659c 100644 --- a/aoc2017-gleam/gleam.toml +++ b/aoc2017-gleam/gleam.toml @@ -18,6 +18,7 @@ gladvent = ">= 0.7.3 and < 1.0.0" gary = ">= 1.0.1 and < 2.0.0" gleam_otp = ">= 0.10.0 and < 1.0.0" gleam_erlang = ">= 0.25.0 and < 1.0.0" +glearray = ">= 0.2.2 and < 1.0.0" [dev-dependencies] gleeunit = ">= 1.0.0 and < 2.0.0" diff --git a/aoc2017-gleam/manifest.toml b/aoc2017-gleam/manifest.toml index cda0b5b..f48e595 100644 --- a/aoc2017-gleam/manifest.toml +++ b/aoc2017-gleam/manifest.toml @@ -29,7 +29,8 @@ packages = [ [requirements] gary = { version = ">= 1.0.1 and < 2.0.0" } gladvent = { version = ">= 0.7.3 and < 1.0.0" } -gleam_erlang = { version = ">= 0.25.0 and < 1.0.0"} +gleam_erlang = { version = ">= 0.25.0 and < 1.0.0" } gleam_otp = { version = ">= 0.10.0 and < 1.0.0" } gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" } +glearray = { version = ">= 0.2.2 and < 1.0.0"} gleeunit = { version = ">= 1.0.0 and < 2.0.0" } diff --git a/aoc2017-gleam/src/aoc_2017/day_17.gleam b/aoc2017-gleam/src/aoc_2017/day_17.gleam new file mode 100644 index 0000000..5904cab --- /dev/null +++ b/aoc2017-gleam/src/aoc_2017/day_17.gleam @@ -0,0 +1,55 @@ +import gleam/int +import gleam/list +import glearray + +pub fn parse(input: String) { + let assert Ok(n) = int.parse(input) + n +} + +pub fn pt_1(input: Int) { + let assert [_, result] = + next_spin([0], 0, 1, input) + |> list.drop_while(fn(x) { x != 2017 }) + |> list.take(2) + + result +} + +fn next_spin(list: List(Int), position: Int, cycle: Int, step: Int) { + case cycle { + 2018 -> list + _ -> { + let next_position = { position + step } % cycle + 1 + next_spin( + insert_at(list, next_position, cycle), + next_position, + cycle + 1, + step, + ) + } + } +} + +fn insert_at(xs: List(a), at index: Int, insert new: a) { + let #(left, right) = list.split(xs, index) + list.concat([left, [new], right]) +} + +pub fn pt_2(input: Int) { + next_spin_tracking_zero(0, 0, 1, input) +} + +fn next_spin_tracking_zero(acc: Int, position: Int, cycle: Int, step: Int) { + case cycle { + 50_000_001 -> acc + _ -> { + let next_position = { position + step } % cycle + 1 + let next_acc = case next_position { + 1 -> cycle + _ -> acc + } + next_spin_tracking_zero(next_acc, next_position, cycle + 1, step) + } + } +} |