-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path15.mjs
42 lines (37 loc) · 994 Bytes
/
15.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
import { readInput } from "./utils.mjs";
const input = readInput(import.meta);
const hashStr = (str) => {
let hashed = 0;
while (str.length) {
hashed += str.charCodeAt(0);
hashed *= 17;
hashed %= 256;
str = str.slice(1);
}
return hashed;
};
const solve1 = (input) =>
input.split(",").reduce((acc, str) => acc + hashStr(str), 0);
const solve2 = (input) => {
const boxes = new Map();
const instructions = input.split(",");
for (const ins of instructions) {
const [label, value] = ins.split(/[=-]/);
const box = hashStr(label);
boxes.set(box, boxes.get(box) ?? new Map());
if (value) {
boxes.get(box).set(label, Number(value));
} else {
boxes.get(box).delete(label);
}
}
let result = 0;
for (const [i, box] of boxes.entries()) {
for (const [j, power] of [...box.values()].entries()) {
result += (i + 1) * (j + 1) * power;
}
}
return result;
};
console.log(solve1(input));
console.log(solve2(input));