aboutsummaryrefslogtreecommitdiff
path: root/codingquest2024/src/day3/solution.gleam
diff options
context:
space:
mode:
authorJ.J <thechairman@thechairman.info>2024-05-30 21:49:58 -0400
committerJ.J <thechairman@thechairman.info>2024-05-30 21:49:58 -0400
commit231c2b688d1e6cf0846d46e883da30e042a9c6cf (patch)
tree98a6d3a461fe190b38b2cf33a708a1d01703fa70 /codingquest2024/src/day3/solution.gleam
parentfe088aa5778dcdbaab4dd8d4a7395a91c444b45c (diff)
parenta2c2b728ec6051323ed937f54816089cd2ae9d20 (diff)
downloadgleam_aoc-231c2b688d1e6cf0846d46e883da30e042a9c6cf.tar.gz
gleam_aoc-231c2b688d1e6cf0846d46e883da30e042a9c6cf.zip
Merge branch 'main' of https://github.com/hunkyjimpjorps/AdventOfCode
Diffstat (limited to 'codingquest2024/src/day3/solution.gleam')
-rw-r--r--codingquest2024/src/day3/solution.gleam50
1 files changed, 50 insertions, 0 deletions
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 {
+ <<slice:bytes-size(100), rest:bytes>> -> {
+ 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
+ }
+}