Files
aoc-jai-2025/day4.jai
2026-06-23 19:39:39 -04:00

68 lines
1.6 KiB
Plaintext

sample :: #string END
..@@.@@@@.
@@@.@.@.@@
@@@@@.@.@@
@.@@@@..@.
@@.@@@@.@@
.@@@@@@@.@
.@.@.@.@@@
@.@@@.@@@@
.@@@@@@@@.
@.@.@@@.@.
END
main :: () {
print("@: %\n", #char "@");
print(".: %\n", #char ".");
content := read_entire_file("day4.input");
solve_first(content);
}
solve_first :: (input: string) {
all_lines := split(input, #char "\n");
lines: [..]string;
for line: all_lines {
if line.count == 0 continue;
array_add(*lines, line);
}
assert(lines[0].count != 0);
line_length := lines[0].count;
removable := 0;
for line, line_index: lines {
if line.count == 0 continue;
for char, char_index: line {
if char == #char "." continue;
surrounding_chars: [8]u8;
surrounding_chars_index := 0;
for dy: -1..1 {
for dx: -1..1 {
if dx == 0 && dy == 0 continue;
defer surrounding_chars_index += 1;
x := char_index + dx;
if x < 0 || line.count <= x continue;
y := line_index + dy;
if y < 0 || lines.count <= y continue;
surrounding_chars[surrounding_chars_index] = lines[y][x];
}
}
if array_count(surrounding_chars, (char: u8) -> bool { return char == #char "@"; }) < 4 {
removable += 1;
}
}
}
print("%\n", removable);
}
array_count :: (arr: []$T, f: (t: T) -> bool) -> int {
total := 0;
for arr {
if f(it) total += 1;
}
return total;
}
#import "Basic";
#import "File";
#import "String";