1const std = @import("std");
2
3pub fn pt1(input: []const u8) u64 {
4 var counter: u64 = 0;
5
6 var lines = std.mem.splitScalar(u8, input, '\n');
7
8 var loop_count: usize = 0;
9 while (lines.next()) |l| {
10 if (l.len == 0) break;
11 for (0..l.len) |y| {
12 board[loop_count][y] = l[y] == '@';
13 }
14 loop_count += 1;
15 }
16
17 for (0..board.len) |x| {
18 for (0..board[x].len) |y| {
19 if (board[x][y] and countSurround(x, y) < 4) {
20 counter += 1;
21 }
22 }
23 }
24
25 return counter;
26}
27
28pub fn pt2(input: []const u8) u64 {
29 var counter: u64 = 0;
30
31 var lines = std.mem.splitScalar(u8, input, '\n');
32
33 var loop_count: usize = 0;
34 while (lines.next()) |l| {
35 if (l.len == 0) break;
36 for (0..l.len) |y| {
37 board[loop_count][y] = l[y] == '@';
38 }
39 loop_count += 1;
40 }
41
42 var hit = true;
43 while (hit) {
44 hit = false;
45 for (0..board.len) |x| {
46 for (0..board[x].len) |y| {
47 if (board[x][y] and countSurround(x, y) < 4) {
48 counter += 1;
49 board[x][y] = false;
50 hit = true;
51 }
52 }
53 }
54 }
55
56 return counter;
57}
58
59var board: [138][138]bool = undefined;
60fn countSurround(rx: usize, ry: usize) i64 {
61 var count: i64 = 0;
62
63 const min_x = if (rx > 0) rx - 1 else 0;
64 const max_x = if (rx + 1 < board.len) rx + 1 else board.len - 1;
65 const min_y = if (ry > 0) ry - 1 else 0;
66 const max_y = if (ry + 1 < board.len) ry + 1 else board.len - 1;
67
68 for (min_x..max_x + 1) |x| {
69 for (min_y..max_y + 1) |y| {
70 if (x == rx and y == ry) continue; // Skip the center cell
71 if (board[x][y]) count += 1;
72 }
73 }
74
75 return count;
76}
77
78test "Day 4 par 1" {
79 const res = pt1(@embedFile("./input/day4"));
80 try std.testing.expect(res == 1549);
81}
82
83test "Day 4 par 2" {
84 const res = pt2(@embedFile("./input/day4"));
85 try std.testing.expect(res == 8887);
86}