Skip to content

Commit 23e1df4

Browse files
committed
day 18 part 2
1 parent df3448d commit 23e1df4

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

2024/Day_18/part_2.js

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,38 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url));
88
// = Copyright (c) NullDev = //
99
// ========================= //
1010

11+
/* eslint-disable no-nested-ternary, no-sequences */
12+
1113
const INPUT = String(fs.readFileSync(path.join(__dirname, "input.txt"))).trim().split("\n");
1214

1315
const pStart = performance.now();
1416

15-
//
16-
// YOUR CODE HERE
17-
//
18-
const result = "...";
17+
const res = ((
18+
grid = Array.from({ length: 71 }, () => Array(71).fill(false)),
19+
pos = INPUT.map(line => line.split(",").map(s => parseInt(s, 10))),
20+
dir = [[0, 1], [0, -1], [1, 0], [-1, 0]],
21+
) => {
22+
const valid = () =>
23+
grid[0][0] ? false : ((
24+
seen = Array.from({ length: 71 }, () => Array(71).fill(false)),
25+
) => {
26+
seen[0][0] = true;
27+
const bfs = front =>
28+
front.some(({ x, y })=>x === 71 - 1 && y === 71 - 1) ? true : front.length === 0
29+
? false : bfs(
30+
front.flatMap(({ x, y }) => dir.map(([dx, dy]) => ({ nx: x + dx, ny: y + dy }))
31+
.filter(({ nx, ny }) => (
32+
nx >= 0 && nx < 71 && ny >= 0 && ny < 71 &&
33+
!grid[ny][nx] && !seen[ny][nx] && (seen[ny][nx] = true)
34+
)).map(({ nx, ny }) => ({ x: nx, y: ny })),
35+
),
36+
);
37+
return bfs([{ x: 0, y: 0 }]);
38+
})();
39+
return pos.find(([x, y]) => ((grid[y][x] = true), !valid()))?.join(",");
40+
})();
1941

2042
const pEnd = performance.now();
2143

22-
console.log("<DESCRIPTION>: " + result);
44+
console.log("MINIMUM NUMBER OF STEPS: " + res);
2345
console.log(pEnd - pStart);

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ I also tried to experiment with a couple of different things:
137137
- 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)
138138
- One-Liner in [Day_14/part_1.js](https://github.com/NullDev/Advent-of-Code/blob/master/2024/Day_14/part_1.js)
139139
- 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)
140+
- _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)
140141

141142
<hr>
142143

0 commit comments

Comments
 (0)