Skip to content

Commit

Permalink
Day 4 [Part 2]
Browse files Browse the repository at this point in the history
  • Loading branch information
Av32000 committed Dec 6, 2024
1 parent 99f9f3e commit f2f363a
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 0 deletions.
64 changes: 64 additions & 0 deletions 04/part2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const { readFileSync } = require("fs");
// Parse Input
const inputFile = "input.txt";
const input = readFileSync(inputFile).toString();
const grid = [];
input.split("\n").forEach((line) => grid.push(line.split("")));

const target = "MAS";
let targetOccurences = [];

const directions = [
[-1, -1],
[1, -1],
[-1, 1],
[1, 1],
];

// Search occurrences
function search(x, y, dx, dy, currentIndex) {
if (currentIndex === target.length) {
targetOccurences.push([x - dx, y - dy]);
return;
}

const newX = x + dx;
const newY = y + dy;

if (
newX >= 0 &&
newY >= 0 &&
newX < grid[0].length &&
newY < grid.length &&
grid[newY][newX] === target[currentIndex]
) {
search(newX, newY, dx, dy, currentIndex + 1);
}
}

for (let y = 0; y < grid.length; y++) {
for (let x = 0; x < grid[0].length; x++) {
if (grid[y][x] === target[0]) {
for (const [dx, dy] of directions) {
search(x, y, dx, dy, 1);
}
}
}
}

const parsed = [];
let result = 0;

targetOccurences.forEach((t) => {
let found = false;
parsed.forEach((p) => {
if (p[0] == t[0] && p[1] == t[1] && !found) {
result++;
found = true;
}
});

if (!found) parsed.push(t);
});

console.log(result);
71 changes: 71 additions & 0 deletions 04/part2.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const { readFileSync } = require("fs");
// Parse Input
const inputFile = "input.txt";
const input = readFileSync(inputFile).toString();
const grid = [];
input.split("\n").forEach((line) => grid.push(line.split("")));

let result = 0;

for (let y = 1; y < grid.length - 1; y++) {
for (let x = 1; x < grid[0].length - 1; x++) {
if (grid[y][x] === "A") {
if (
// Case 1
(grid[y - 1][x - 1] === "M" &&
grid[y + 1][x + 1] === "S" &&
grid[y - 1][x + 1] === "S" &&
grid[y + 1][x - 1] === "M") ||
// Case 2
(grid[y - 1][x - 1] === "S" &&
grid[y + 1][x + 1] === "M" &&
grid[y - 1][x + 1] === "M" &&
grid[y + 1][x - 1] === "S") ||
// Case 3
(grid[y - 1][x - 1] === "M" &&
grid[y + 1][x + 1] === "S" &&
grid[y - 1][x + 1] === "M" &&
grid[y + 1][x - 1] === "S") ||
// Case 4
(grid[y - 1][x - 1] === "S" &&
grid[y + 1][x + 1] === "M" &&
grid[y - 1][x + 1] === "S" &&
grid[y + 1][x - 1] === "M") ||
// Case 5
(grid[y - 1][x - 1] === "M" &&
grid[y + 1][x + 1] === "M" &&
grid[y - 1][x + 1] === "S" &&
grid[y + 1][x - 1] === "S") ||
// Case 6
(grid[y - 1][x - 1] === "S" &&
grid[y + 1][x + 1] === "S" &&
grid[y - 1][x + 1] === "M" &&
grid[y + 1][x - 1] === "M") ||
// Case 7
(grid[y - 1][x - 1] === "S" &&
grid[y + 1][x + 1] === "M" &&
grid[y - 1][x + 1] === "M" &&
grid[y + 1][x - 1] === "S") ||
// Case 8
(grid[y - 1][x - 1] === "M" &&
grid[y + 1][x + 1] === "S" &&
grid[y - 1][x + 1] === "S" &&
grid[y + 1][x - 1] === "M") ||
// Case 9
(grid[y - 1][x - 1] === "S" &&
grid[y + 1][x + 1] === "M" &&
grid[y - 1][x + 1] === "M" &&
grid[y + 1][x - 1] === "S") ||
// Case 10
(grid[y - 1][x - 1] === "M" &&
grid[y + 1][x + 1] === "S" &&
grid[y - 1][x + 1] === "S" &&
grid[y + 1][x - 1] === "M")
) {
result += 1;
}
}
}
}

console.log(result);

0 comments on commit f2f363a

Please sign in to comment.