my solutions to advent of code
aoc advent-of-code

part 2

part 2

part 2

part 2

aylac.top cb0c6636 46604370

verified
Changed files
+58 -22
2025
4
gleam
+58 -22
2025/4/gleam/src/main.gleam
···
import gleam/string
import simplifile as file
-
fn solve(input, size) {
-
let get_place = fn(x, y) {
-
case
-
case x, y {
-
_, -1 | -1, _ -> <<>>
-
s, _ | _, s if s == size -> <<>>
+
fn get_place(x, y, map, size) {
+
case
+
case x, y {
+
_, -1 | -1, _ -> <<>>
+
s, _ | _, s if s == size -> <<>>
-
_, _ -> bit_array.slice(input, y * size + x, 1) |> result.unwrap(<<>>)
-
}
-
{
-
<<"@">> -> 1
-
_ -> 0
+
_, _ -> bit_array.slice(map, y * size + x, 1) |> result.unwrap(<<>>)
}
+
{
+
<<"@">> -> 1
+
_ -> 0
}
+
}
-
list.range(0, bit_array.byte_size(input) - 1)
+
fn part_1(map, size) {
+
list.range(0, bit_array.byte_size(map) - 1)
|> list.fold(0, fn(acc, pos) {
let x = pos % size
let y = pos / size
-
let roll = get_place(x, y)
+
let roll = get_place(x, y, map, size)
let neighbours =
-
get_place(x - 1, y - 1)
-
+ get_place(x, y - 1)
-
+ get_place(x + 1, y - 1)
-
+ get_place(x - 1, y)
-
+ get_place(x + 1, y)
-
+ get_place(x - 1, y + 1)
-
+ get_place(x, y + 1)
-
+ get_place(x + 1, y + 1)
+
get_place(x - 1, y - 1, map, size)
+
+ get_place(x, y - 1, map, size)
+
+ get_place(x + 1, y - 1, map, size)
+
+ get_place(x - 1, y, map, size)
+
+ get_place(x + 1, y, map, size)
+
+ get_place(x - 1, y + 1, map, size)
+
+ get_place(x, y + 1, map, size)
+
+ get_place(x + 1, y + 1, map, size)
case roll, neighbours < 4 {
1, True -> acc + 1
···
})
}
+
fn part_2(map, size, rolls) {
+
let #(rolls, new_map) =
+
list.range(0, bit_array.byte_size(map) - 1)
+
|> list.fold(#(rolls, <<>>), fn(acc, pos) {
+
let #(total, new_map) = acc
+
let x = pos % size
+
let y = pos / size
+
+
let roll = get_place(x, y, map, size)
+
+
let neighbours =
+
get_place(x - 1, y - 1, map, size)
+
+ get_place(x, y - 1, map, size)
+
+ get_place(x + 1, y - 1, map, size)
+
+ get_place(x - 1, y, map, size)
+
+ get_place(x + 1, y, map, size)
+
+ get_place(x - 1, y + 1, map, size)
+
+ get_place(x, y + 1, map, size)
+
+ get_place(x + 1, y + 1, map, size)
+
+
case roll, neighbours < 4 {
+
1, True -> #(total + 1, bit_array.append(new_map, <<".">>))
+
0, _ -> #(total, bit_array.append(new_map, <<".">>))
+
1, _ | _, _ -> #(total, bit_array.append(new_map, <<"@">>))
+
}
+
})
+
+
case map == new_map {
+
True -> rolls
+
False -> part_2(new_map, size, rolls)
+
}
+
}
+
pub fn main() {
let assert Ok(input) = file.read(from: "../input.txt")
as "Input file not found"
···
let input = input |> list.fold(<<>>, fn(acc, l) { bit_array.append(acc, l) })
// @ and .
-
solve(input, size)
+
part_1(input, size)
+
|> int.to_string
+
|> io.println
+
part_2(input, size, 0)
|> int.to_string
|> io.println
}