Skip to content

Commit

Permalink
day 18 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
NullDev committed Dec 18, 2024
1 parent df3448d commit 23e1df4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
32 changes: 27 additions & 5 deletions 2024/Day_18/part_2.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,38 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url));
// = Copyright (c) NullDev = //
// ========================= //

/* eslint-disable no-nested-ternary, no-sequences */

const INPUT = String(fs.readFileSync(path.join(__dirname, "input.txt"))).trim().split("\n");

const pStart = performance.now();

//
// YOUR CODE HERE
//
const result = "...";
const res = ((
grid = Array.from({ length: 71 }, () => Array(71).fill(false)),
pos = INPUT.map(line => line.split(",").map(s => parseInt(s, 10))),
dir = [[0, 1], [0, -1], [1, 0], [-1, 0]],
) => {
const valid = () =>
grid[0][0] ? false : ((
seen = Array.from({ length: 71 }, () => Array(71).fill(false)),
) => {
seen[0][0] = true;
const bfs = front =>
front.some(({ x, y })=>x === 71 - 1 && y === 71 - 1) ? true : front.length === 0
? false : bfs(
front.flatMap(({ x, y }) => dir.map(([dx, dy]) => ({ nx: x + dx, ny: y + dy }))
.filter(({ nx, ny }) => (
nx >= 0 && nx < 71 && ny >= 0 && ny < 71 &&
!grid[ny][nx] && !seen[ny][nx] && (seen[ny][nx] = true)
)).map(({ nx, ny }) => ({ x: nx, y: ny })),
),
);
return bfs([{ x: 0, y: 0 }]);
})();
return pos.find(([x, y]) => ((grid[y][x] = true), !valid()))?.join(",");
})();

const pEnd = performance.now();

console.log("<DESCRIPTION>: " + result);
console.log("MINIMUM NUMBER OF STEPS: " + res);
console.log(pEnd - pStart);
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ I also tried to experiment with a couple of different things:
- One-Liner in both [Day_13/part_1.js](https://github.com/NullDev/Advent-of-Code/blob/master/2024/Day_13/part_1.js) and [Day_13/part_2.js](https://github.com/NullDev/Advent-of-Code/blob/master/2024/Day_13/part_2.js)
- One-Liner in [Day_14/part_1.js](https://github.com/NullDev/Advent-of-Code/blob/master/2024/Day_14/part_1.js)
- Sloppy, hacky, small, quick & dirty dijkstra in [Day_16/part_1.js](https://github.com/NullDev/Advent-of-Code/blob/master/2024/Day_16/part_1.js)
- _Almost_ One-Liner [BFS](https://en.wikipedia.org/wiki/Breadth-first_search) in [Day_18/part_1.js](https://github.com/NullDev/Advent-of-Code/blob/master/2024/Day_18/part_1.js)

<hr>

Expand Down

0 comments on commit 23e1df4

Please sign in to comment.