From 2a5e033b2d8882f50f2bfe4da25906c57f2a41f0 Mon Sep 17 00:00:00 2001 From: "H.J" Date: Thu, 7 Mar 2024 15:50:04 -0500 Subject: CodingQuest days 3 and 4 in Gleam --- codingquest2024/src/day3/solution.gleam | 50 +++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 codingquest2024/src/day3/solution.gleam (limited to 'codingquest2024/src/day3/solution.gleam') diff --git a/codingquest2024/src/day3/solution.gleam b/codingquest2024/src/day3/solution.gleam new file mode 100644 index 0000000..6314060 --- /dev/null +++ b/codingquest2024/src/day3/solution.gleam @@ -0,0 +1,50 @@ +import gleam/bit_array +import gleam/int +import gleam/io +import gleam/string +import gleam/string_builder.{type StringBuilder} +import simplifile + +pub fn main() { + let assert Ok(data) = simplifile.read(from: "./src/day3/data.txt") + + data + |> string.split(" ") + |> build_flat_image(" ", string_builder.new()) + |> bit_array.from_string() + |> print_next_slice() +} + +fn build_flat_image( + nums: List(String), + pixel: String, + acc: StringBuilder, +) -> String { + case nums { + [] -> string_builder.to_string(acc) + [h, ..t] -> { + let assert Ok(n) = int.parse(h) + let pixels = string.repeat(pixel, n) + build_flat_image(t, next_pixel(pixel), string_builder.append(acc, pixels)) + } + } +} + +fn print_next_slice(str: BitArray) -> Nil { + case str { + <> -> { + let assert Ok(out) = bit_array.to_string(slice) + io.println(out) + print_next_slice(rest) + } + _ -> Nil + } +} + +fn next_pixel(p: String) -> String { + case p { + " " -> "#" + "#" -> " " + _ -> panic + } +} -- cgit v1.2.3