Skip to content

Commit 4619ba5

Browse files
committed
feat: 2025 Day 4
1 parent 2094b7b commit 4619ba5

4 files changed

Lines changed: 86 additions & 0 deletions

File tree

2025/day/4/part/1/solve.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import solve from "./solve.ts";
2+
3+
import { assertEquals } from "@std/assert";
4+
5+
Deno.test("example", () => {
6+
const input = `\
7+
..@@.@@@@.
8+
@@@.@.@.@@
9+
@@@@@.@.@@
10+
@.@@@@..@.
11+
@@.@@@@.@@
12+
.@@@@@@@.@
13+
.@.@.@.@@@
14+
@.@@@.@@@@
15+
.@@@@@@@@.
16+
@.@.@@@.@.`;
17+
18+
assertEquals(solve(input), 13);
19+
});

2025/day/4/part/1/solve.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// deno-fmt-ignore
2+
const directions = [
3+
[-1, -1], [ 0, -1], [ 1, -1],
4+
[-1, 0], [ 1, 0],
5+
[-1, 1], [ 0, 1], [ 1, 1],
6+
];
7+
8+
export default function solve(input: string) {
9+
const grid = input.split("\n").map((line) => Array.from(line));
10+
let count = 0;
11+
for (let y = 0; y < grid.length; y++) {
12+
for (let x = 0; x < grid[y].length; x++) {
13+
if (grid[y][x] !== "@") continue;
14+
const { length: adjacentCount } = directions.filter(([dx, dy]) =>
15+
grid[y + dy]?.[x + dx] === "@"
16+
);
17+
if (adjacentCount < 4) count++;
18+
}
19+
}
20+
return count;
21+
}

2025/day/4/part/2/solve.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import solve from "./solve.ts";
2+
3+
import { assertEquals } from "@std/assert";
4+
5+
Deno.test("example", () => {
6+
const input = `\
7+
..@@.@@@@.
8+
@@@.@.@.@@
9+
@@@@@.@.@@
10+
@.@@@@..@.
11+
@@.@@@@.@@
12+
.@@@@@@@.@
13+
.@.@.@.@@@
14+
@.@@@.@@@@
15+
.@@@@@@@@.
16+
@.@.@@@.@.`;
17+
18+
assertEquals(solve(input), 43);
19+
});

2025/day/4/part/2/solve.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// deno-fmt-ignore
2+
const directions = [
3+
[-1, -1], [ 0, -1], [ 1, -1],
4+
[-1, 0], [ 1, 0],
5+
[-1, 1], [ 0, 1], [ 1, 1],
6+
];
7+
8+
export default function solve(input: string) {
9+
const grid = input.split("\n").map((line) => Array.from(line));
10+
let count = 0;
11+
while (true) {
12+
const toRemove: [number, number][] = [];
13+
for (let y = 0; y < grid.length; y++) {
14+
for (let x = 0; x < grid[y].length; x++) {
15+
if (grid[y][x] !== "@") continue;
16+
const { length: adjacentCount } = directions.filter(([dx, dy]) =>
17+
grid[y + dy]?.[x + dx] === "@"
18+
);
19+
if (adjacentCount < 4) toRemove.push([x, y]);
20+
}
21+
}
22+
if (toRemove.length === 0) break;
23+
count += toRemove.length;
24+
for (const [x, y] of toRemove) grid[y][x] = "x";
25+
}
26+
return count;
27+
}

0 commit comments

Comments
 (0)