blob: 6314060d1db951299715ebbbe2e0c671390dfe5a (
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
}
}
|