diff options
author | H.J <thechairman@thechairman.info> | 2024-10-09 11:36:55 -0400 |
---|---|---|
committer | H.J <thechairman@thechairman.info> | 2024-10-09 11:36:55 -0400 |
commit | 8777ff071f7bb37631baa7b6717ad29961e50911 (patch) | |
tree | 6d59c4ed58e454b960339c3d1151f0a879e8d7cb /aoc2017-gleam/src/aoc_2017/day_11.gleam | |
parent | 6156a9ef7be4012063a042aafb4e9b0d7eadde8e (diff) | |
download | gleam_aoc-8777ff071f7bb37631baa7b6717ad29961e50911.tar.gz gleam_aoc-8777ff071f7bb37631baa7b6717ad29961e50911.zip |
sorting by language
Diffstat (limited to 'aoc2017-gleam/src/aoc_2017/day_11.gleam')
-rw-r--r-- | aoc2017-gleam/src/aoc_2017/day_11.gleam | 75 |
1 files changed, 0 insertions, 75 deletions
diff --git a/aoc2017-gleam/src/aoc_2017/day_11.gleam b/aoc2017-gleam/src/aoc_2017/day_11.gleam deleted file mode 100644 index 7d3df0b..0000000 --- a/aoc2017-gleam/src/aoc_2017/day_11.gleam +++ /dev/null @@ -1,75 +0,0 @@ -import gleam/int -import gleam/list -import gleam/string - -pub type Direction { - North - Northeast - Northwest - South - Southeast - Southwest -} - -type HexPosition { - HexPosition(x: Int, y: Int, z: Int) -} - -const start = HexPosition(0, 0, 0) - -fn to_direction(str: String) -> Direction { - case str { - "n" -> North - "ne" -> Northeast - "nw" -> Northwest - "s" -> South - "se" -> Southeast - "sw" -> Southwest - _ -> panic as "unrecognized direction" - } -} - -fn distance(hp1: HexPosition, hp2: HexPosition) -> Int { - { - int.absolute_value(hp1.x - hp2.x) - + int.absolute_value(hp1.y - hp2.y) - + int.absolute_value(hp1.z - hp2.z) - } - / 2 -} - -fn move(p, direction) -> HexPosition { - case direction { - North -> HexPosition(..p, y: p.y + 1, z: p.z - 1) - South -> HexPosition(..p, y: p.y - 1, z: p.z + 1) - Northeast -> HexPosition(..p, x: p.x + 1, z: p.z - 1) - Southwest -> HexPosition(..p, x: p.x - 1, z: p.z + 1) - Southeast -> HexPosition(..p, x: p.x + 1, y: p.y - 1) - Northwest -> HexPosition(..p, x: p.x - 1, y: p.y + 1) - } -} - -pub fn parse(input: String) -> List(Direction) { - input - |> string.split(",") - |> list.map(to_direction) -} - -pub fn pt_1(input: List(Direction)) { - do_walk(input, start, 0).0 -} - -pub fn pt_2(input: List(Direction)) { - do_walk(input, start, 0).1 -} - -fn do_walk(steps, position, max) { - case steps { - [] -> #(distance(position, HexPosition(0, 0, 0)), max) - [next, ..rest] -> { - let new_position = move(position, next) - let new_max = int.max(max, distance(new_position, start)) - do_walk(rest, new_position, new_max) - } - } -} |