-
Notifications
You must be signed in to change notification settings - Fork 5
/
day03.mjs
50 lines (39 loc) · 1.33 KB
/
day03.mjs
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
48
49
50
import { readFileSync } from "node:fs";
const lines = readFileSync("day03.txt", { encoding: "utf-8" }) // read day??.txt content
.replace(/\r/g, "") // remove all \r characters to avoid issues on Windows
.trim() // Remove starting/ending whitespace
.split("\n"); // Split on newline
function letterToPriority(letter) {
if (/[a-z]/.test(letter)) {
//lowercase
return letter.charCodeAt(0) - 96;
} else {
return letter.charCodeAt(0) - 65 + 27;
}
}
function part1() {
const res = lines.map((line) => {
const part1 = [...line.slice(0, line.length / 2)];
const part2 = [...line.slice(line.length / 2)];
let part1Set = new Set(part1);
const intersection = part2.filter((x) => part1Set.has(x));
const dedup = [...new Set(intersection)];
return letterToPriority(dedup[0]);
});
console.log(res.reduce((a, b) => a + b, 0));
}
function part2() {
let sum = 0;
for (let i = 0; i < lines.length; i += 3) {
const backpacks = [[...lines[i]], [...lines[i + 1]], [...lines[i + 2]]];
let set = new Set(backpacks[0]);
let intersection = backpacks[1].filter((x) => set.has(x));
set = new Set(intersection);
intersection = backpacks[2].filter((x) => set.has(x));
const dedup = [...new Set(intersection)];
sum += letterToPriority(dedup[0]);
}
console.log(sum);
}
part1();
part2();