diff options
author | Tomasz Chojnacki <tomaszchojnacki2001@gmail.com> | 2023-04-06 14:41:14 +0200 |
---|---|---|
committer | Tomasz Chojnacki <tomaszchojnacki2001@gmail.com> | 2023-04-06 14:41:14 +0200 |
commit | 20852d6e60bee896e84c9bae77f45211773dbf60 (patch) | |
tree | 9e952048527a2c97a2dcd19dbdeb1d6657b5b1f1 /aoc-2020-gleam/src/util/dir.gleam | |
parent | bcd24674ec0a594882b276fd48fe70c6e3d7ec86 (diff) | |
download | gleam_aoc2020-20852d6e60bee896e84c9bae77f45211773dbf60.tar.gz gleam_aoc2020-20852d6e60bee896e84c9bae77f45211773dbf60.zip |
Finish day 12
Diffstat (limited to 'aoc-2020-gleam/src/util/dir.gleam')
-rw-r--r-- | aoc-2020-gleam/src/util/dir.gleam | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/aoc-2020-gleam/src/util/dir.gleam b/aoc-2020-gleam/src/util/dir.gleam new file mode 100644 index 0000000..6f637d9 --- /dev/null +++ b/aoc-2020-gleam/src/util/dir.gleam @@ -0,0 +1,41 @@ +import gleam/int +import gleam/iterator as iter +import ext/resultx as resx +import ext/iteratorx as iterx +import util/pos.{Pos} + +pub type Dir { + North + East + South + West +} + +pub fn offset(direction: Dir) -> Pos { + case direction { + North -> #(0, 1) + East -> #(1, 0) + South -> #(0, -1) + West -> #(-1, 0) + } +} + +pub fn degree_to_turn(degree: Int) -> Int { + resx.assert_unwrap(int.modulo(degree / 90, by: 4)) +} + +fn rotate_clockwise_once(direction: Dir) -> Dir { + case direction { + North -> East + East -> South + South -> West + West -> North + } +} + +pub fn rotate_clockwise(this direction: Dir, by times: Int) -> Dir { + direction + |> iterx.unfold_infinitely(with: rotate_clockwise_once) + |> iter.at(times) + |> resx.assert_unwrap +} |