aboutsummaryrefslogtreecommitdiff
path: root/aoc2023/src/utilities/array2d.gleam
blob: 1ad824cc59ae26642adba100d0e241b2c18f038a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
}