aboutsummaryrefslogtreecommitdiff
path: root/aoc2023/src/utilities
diff options
context:
space:
mode:
Diffstat (limited to 'aoc2023/src/utilities')
-rw-r--r--aoc2023/src/utilities/array2d.gleam27
1 files changed, 27 insertions, 0 deletions
diff --git a/aoc2023/src/utilities/array2d.gleam b/aoc2023/src/utilities/array2d.gleam
new file mode 100644
index 0000000..1ad824c
--- /dev/null
+++ b/aoc2023/src/utilities/array2d.gleam
@@ -0,0 +1,27 @@
+import gleam/list
+import gleam/dict.{type Dict}
+import gleam/string
+
+pub type Posn {
+ Posn(x: Int, y: Int)
+}
+
+pub type Array2D(a) =
+ Dict(Posn, a)
+
+pub fn to_2d_array(xss: List(List(a))) -> Array2D(a) {
+ {
+ use x, row <- list.index_map(xss)
+ use y, cell <- list.index_map(row)
+ #(Posn(x, y), cell)
+ }
+ |> list.flatten
+ |> dict.from_list
+}
+
+pub fn parse_grid(str: String) -> Array2D(String) {
+ str
+ |> string.split("\n")
+ |> list.map(string.to_graphemes)
+ |> to_2d_array
+}