-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpart_2.js
More file actions
47 lines (40 loc) · 1.44 KB
/
part_2.js
File metadata and controls
47 lines (40 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// @ts-nocheck
import fs from "node:fs";
import path from "node:path";
import { performance } from "node:perf_hooks";
import { fileURLToPath } from "url";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
// ========================= //
// = Copyright (c) NullDev = //
// ========================= //
/* eslint-disable no-sequences */
const INPUT = String(fs.readFileSync(path.join(__dirname, "input.txt")))
.replace(/\n$/, "").split(/\n/);
const pStart = performance.now();
const res = ((
width = Math.max(...INPUT.map(l => l.length)),
grid = INPUT.map(l => l.padEnd(width, " ")),
) => Array.from({ length: width }, (_, c) =>
grid.some(row => row[c] !== " "),
).reduce((acc, isContent, idx, arr) => (
((isContent && (idx === 0 || !arr[idx - 1]))
? acc.push([idx, idx])
: (acc[acc.length - 1][1] = idx)), acc
), []).map((
[start, end], _, __,
cols = Array.from({
length: end - start + 1,
}, (_n, i) => start + i),
op = grid[INPUT.length - 1].slice(start, end + 1).includes("+") ? "+" : "*",
) => cols.slice()
.reverse()
.map(c => Array.from({
length: INPUT.length - 1,
}, (_n, r) => grid[r][c]).join("").trim())
.filter(Boolean)
.map(Number)
.reduce((acc, n) => (op === "+" ? acc + n : acc * n), op === "+" ? 0 : 1),
).reduce((a, b) => a + b, 0))();
const pEnd = performance.now();
console.log("TOTAL OF ANSWERS (RTL): " + res);
console.log(pEnd - pStart);