Skip to content

Instantly share code, notes, and snippets.

@kirkegaard
Created December 4, 2024 09:33
Show Gist options
  • Save kirkegaard/d38d322ae8c886a6cbd14623a08fa942 to your computer and use it in GitHub Desktop.
Save kirkegaard/d38d322ae8c886a6cbd14623a08fa942 to your computer and use it in GitHub Desktop.
advent of code day 4
const input = await Deno.readTextFile("input.txt");
const directions = [
[0, 1],
[0, -1],
[1, 0],
[1, 1],
[1, -1],
[-1, 0],
[-1, -1],
[-1, 1],
];
const grid = input.split("\n").map((row) => row.split(""));
const xmas = "XMAS";
let total_1 = 0;
for (let row = 0; row < grid.length; row++) {
for (let col = 0; col < grid[row].length; col++) {
for (const dir of directions) {
for (let w = 0; w < xmas.length; w++) {
const r = row + dir[0] * w;
const c = col + dir[1] * w;
if (r < 0 || r >= grid.length || c < 0 || c >= grid[r].length) {
break;
}
if (grid[r][c] !== xmas[w]) {
break;
}
if (w === xmas.length - 1) {
total_1++;
}
}
}
}
}
let total_2 = 0;
for (let row = 0; row < grid.length; row++) {
for (let col = 0; col < grid[row].length; col++) {
if (grid[row][col] === "A") {
try {
const lr = [grid[row - 1][col - 1], grid[row + 1][col + 1]];
const rl = [grid[row - 1][col + 1], grid[row + 1][col - 1]];
if (
lr.includes("M") &&
lr.includes("S") &&
rl.includes("M") &&
rl.includes("S")
) {
total_2++;
}
} catch {
break;
}
}
}
}
console.log({ total_1, total_2 });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment