···
import simplifile as file
9
-
fn solve(input, size) {
10
-
let get_place = fn(x, y) {
13
-
_, -1 | -1, _ -> <<>>
14
-
s, _ | _, s if s == size -> <<>>
9
+
fn get_place(x, y, map, size) {
12
+
_, -1 | -1, _ -> <<>>
13
+
s, _ | _, s if s == size -> <<>>
16
-
_, _ -> bit_array.slice(input, y * size + x, 1) |> result.unwrap(<<>>)
15
+
_, _ -> bit_array.slice(map, y * size + x, 1) |> result.unwrap(<<>>)
24
-
list.range(0, bit_array.byte_size(input) - 1)
23
+
fn part_1(map, size) {
24
+
list.range(0, bit_array.byte_size(map) - 1)
|> list.fold(0, fn(acc, pos) {
29
-
let roll = get_place(x, y)
29
+
let roll = get_place(x, y, map, size)
32
-
get_place(x - 1, y - 1)
33
-
+ get_place(x, y - 1)
34
-
+ get_place(x + 1, y - 1)
35
-
+ get_place(x - 1, y)
36
-
+ get_place(x + 1, y)
37
-
+ get_place(x - 1, y + 1)
38
-
+ get_place(x, y + 1)
39
-
+ get_place(x + 1, y + 1)
32
+
get_place(x - 1, y - 1, map, size)
33
+
+ get_place(x, y - 1, map, size)
34
+
+ get_place(x + 1, y - 1, map, size)
35
+
+ get_place(x - 1, y, map, size)
36
+
+ get_place(x + 1, y, map, size)
37
+
+ get_place(x - 1, y + 1, map, size)
38
+
+ get_place(x, y + 1, map, size)
39
+
+ get_place(x + 1, y + 1, map, size)
case roll, neighbours < 4 {
···
48
+
fn part_2(map, size, rolls) {
49
+
let #(rolls, new_map) =
50
+
list.range(0, bit_array.byte_size(map) - 1)
51
+
|> list.fold(#(rolls, <<>>), fn(acc, pos) {
52
+
let #(total, new_map) = acc
56
+
let roll = get_place(x, y, map, size)
59
+
get_place(x - 1, y - 1, map, size)
60
+
+ get_place(x, y - 1, map, size)
61
+
+ get_place(x + 1, y - 1, map, size)
62
+
+ get_place(x - 1, y, map, size)
63
+
+ get_place(x + 1, y, map, size)
64
+
+ get_place(x - 1, y + 1, map, size)
65
+
+ get_place(x, y + 1, map, size)
66
+
+ get_place(x + 1, y + 1, map, size)
68
+
case roll, neighbours < 4 {
69
+
1, True -> #(total + 1, bit_array.append(new_map, <<".">>))
70
+
0, _ -> #(total, bit_array.append(new_map, <<".">>))
71
+
1, _ | _, _ -> #(total, bit_array.append(new_map, <<"@">>))
75
+
case map == new_map {
77
+
False -> part_2(new_map, size, rolls)
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) })
93
+
part_2(input, size, 0)