Day 4
This commit is contained in:
parent
a2f09799e5
commit
f1a2c33fce
2 changed files with 36 additions and 0 deletions
19
day4/part1.ts
Normal file
19
day4/part1.ts
Normal file
|
@ -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
|
||||||
|
}
|
17
day4/part2.ts
Normal file
17
day4/part2.ts
Normal file
|
@ -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
|
||||||
|
}
|
Loading…
Reference in a new issue