Skip to content

Commit a8eba69

Browse files
committed
Day 14 part 2
1 parent d6e3a76 commit a8eba69

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

2024/Day_14/part_2.js

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

11-
const INPUT = String(fs.readFileSync(path.join(__dirname, "input.txt"))).trim().split("\n");
11+
const INPUT = String(fs.readFileSync(path.join(__dirname, "input.txt"))).trim().split("\n")
12+
.map(l => l.split(" ").map(p => p.substring(2).split(",").map(Number)));
1213

1314
const pStart = performance.now();
1415

15-
//
16-
// YOUR CODE HERE
17-
//
18-
const result = "...";
16+
let res = -1;
17+
for (
18+
let i = 0, score = Infinity,
19+
lcm = (
20+
a, b,
21+
gcd = (x, y) => !y ? x : gcd(y, x % y),
22+
) => a / gcd(a, b) * b;
23+
i < lcm(...[101, 103]); i++
24+
){
25+
const t = ((
26+
pMap = INPUT.map(r => r[0].map((pc, i1) => (
27+
((pc + r[1][i1] * i) % [101, 103][i1]) + [101, 103][i1]) % [101, 103][i1])),
28+
qs = [101, 103].map(c => Math.floor(c / 2)),
29+
ac = (
30+
pos, origin, size, max = origin.map((c, i2) => c + size[i2]),
31+
) => pos.reduce((s, p) => s + (p.every((c, i3) => c >= origin[i3] && c < max[i3]) ? 1 : 0), 0),
32+
) => (ac(pMap, [0, 0], qs) *
33+
ac(pMap, [qs[0] + 1, 0], qs) *
34+
ac(pMap, [0, qs[1] + 1], qs) *
35+
ac(pMap, qs.map(c => c + 1), qs)
36+
))();
37+
(t < score) && (score = t) && (res = i);
38+
}
1939

2040
const pEnd = performance.now();
2141

22-
console.log("<DESCRIPTION>: " + result);
42+
console.log("SECONDS TO ELAPSE: " + res);
2343
console.log(pEnd - pStart);

0 commit comments

Comments
 (0)