From f1a2c33fce8a09c63883cf40b58fc4cfb065708c Mon Sep 17 00:00:00 2001 From: Tyler Beckman Date: Tue, 3 Dec 2024 21:42:57 -0700 Subject: [PATCH] Day 4 --- day4/part1.ts | 19 +++++++++++++++++++ day4/part2.ts | 17 +++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 day4/part1.ts create mode 100644 day4/part2.ts diff --git a/day4/part1.ts b/day4/part1.ts new file mode 100644 index 0000000..912213f --- /dev/null +++ b/day4/part1.ts @@ -0,0 +1,19 @@ +export async function solve() { + const grid = await Deno.readTextFile("./input.txt").then(i => i.split("\n").map(l => l.split(""))); + let sum = 0; + + for (const [y, line] of grid.map((e, i) => [i, e] as const)) { + for (let x = 0; x < line.length; x++) { + // Check horizontal forwards & backwards + if (["XMAS", "SAMX"].includes(grid[y].slice(x, x + 4).join(""))) sum++; + // Check vertical forwards & backwards + if (["XMAS", "SAMX"].includes(grid.slice(y, y + 4).map(l => l[x]).join(""))) sum++; + // Check diagonal \ forwards & backwards + if (["XMAS", "SAMX"].includes(grid.slice(y, y + 4).map((l, i) => l[x + i]).join(""))) sum++; + // Check diagonal / forwards & backwards + if (["XMAS", "SAMX"].includes(grid.slice(y, y + 4).map((l, i) => l[x + (3 - i)]).join(""))) sum++; + } + } + + return sum +} diff --git a/day4/part2.ts b/day4/part2.ts new file mode 100644 index 0000000..c8ae6f5 --- /dev/null +++ b/day4/part2.ts @@ -0,0 +1,17 @@ +export async function solve() { + const grid = await Deno.readTextFile("./input.txt").then(i => i.split("\n").map(l => l.split(""))); + let sum = 0; + + for (const [y, line] of grid.map((e, i) => [i, e] as const)) { + for (let x = 0; x < line.length; x++) { + if ( + // Check diagonal \ forwards & backwards + ["MAS", "SAM"].includes(grid.slice(y, y + 3).map((l, i) => l[x + i]).join("")) + // Check diagonal / forwards & backwards + && ["MAS", "SAM"].includes(grid.slice(y, y + 3).map((l, i) => l[x + 2 - i]).join("")) + ) sum++; + } + } + + return sum +}