aboutsummaryrefslogtreecommitdiff
path: root/aoc-2020-gleam/src/util/grid.gleam
blob: a091be5d93c633ebe51265f416562eec8e630b8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import gleam/list
import gleam/string as str
import gleam/set.{type Set}

pub fn parse_grid(lines: String, with constructor: fn(Int, Int) -> a) -> Set(a) {
  lines
  |> str.split("\n")
  |> list.index_map(with: fn(line, y) {
    line
    |> str.to_graphemes
    |> list.index_map(with: fn(grapheme, x) {
      case grapheme {
        "#" -> [constructor(x, y)]
        "." -> []
        _ -> panic
      }
    })
    |> list.flatten
  })
  |> list.flatten
  |> set.from_list
}