-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpart_2.js
More file actions
37 lines (30 loc) · 1.15 KB
/
part_2.js
File metadata and controls
37 lines (30 loc) · 1.15 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
// @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-return-assign, no-sequences, no-nested-ternary */
const [r] = String(fs.readFileSync(path.join(__dirname, "input.txt")))
.trim().split("\n\n");
const pStart = performance.now();
const res = r
.split("\n")
.map(l => l.trim())
.filter(l => l.trim().length > 0)
.map((l, _, __, [start, end] = l.split("-").map(Number)) => ([start, end]))
.sort((a, b) => a[0] - b[0])
.reduce((
acc, [start, end], _, __, last = acc[acc.length - 1],
) => (((!acc.length)
? (acc.push([start, end]), acc)
: ((start <= last[1] + 1)
? last[1] = Math.max(last[1], end)
: acc.push([start, end]))), acc), [])
.reduce((sum, [start, end]) => sum + (end - start + 1), 0);
const pEnd = performance.now();
console.log("FRESH UNIQUE ID COUNT: " + res);
console.log(pEnd - pStart);