+28
day02/part2.ts
+28
day02/part2.ts
···
1
+
import { readFileSync } from "fs";
2
+
3
+
const ranges = readFileSync("./input.txt", "utf8")
4
+
.split(",")
5
+
.map((range) => {
6
+
const [first, last] = range.split("-");
7
+
return { first: Number(first), last: Number(last) };
8
+
});
9
+
10
+
let invalidIDsSum = 0;
11
+
12
+
for (const { first, last } of ranges) {
13
+
for (let i = first; i <= last; i++) {
14
+
const id = String(i);
15
+
16
+
for (let j = 1; j <= id.length / 2; j++) {
17
+
const chunks = new Set(id.match(new RegExp(`.{1,${j}}`, "g")));
18
+
19
+
if (chunks.size === 1) {
20
+
invalidIDsSum += i;
21
+
22
+
break;
23
+
}
24
+
}
25
+
}
26
+
}
27
+
28
+
console.log(invalidIDsSum);