aboutsummaryrefslogtreecommitdiff
path: root/aoc-2020-gleam/src/util/grid.gleam
diff options
context:
space:
mode:
Diffstat (limited to 'aoc-2020-gleam/src/util/grid.gleam')
-rw-r--r--aoc-2020-gleam/src/util/grid.gleam22
1 files changed, 22 insertions, 0 deletions
diff --git a/aoc-2020-gleam/src/util/grid.gleam b/aoc-2020-gleam/src/util/grid.gleam
new file mode 100644
index 0000000..a091be5
--- /dev/null
+++ b/aoc-2020-gleam/src/util/grid.gleam
@@ -0,0 +1,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
+}