-
Notifications
You must be signed in to change notification settings - Fork 0
/
day15.ts
47 lines (39 loc) · 1.11 KB
/
day15.ts
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
import _ from "lodash";
import fs from "fs";
const line: string = fs.readFileSync("inputs/day15.txt", "utf-8").trimEnd();
const steps: string[] = line.split(",");
const hash = (str: string): number =>
_.reduce(str, (acc, c) => ((acc + c.charCodeAt(0)) * 17) % 256, 0);
// Part 1
console.log(_.sum(steps.map(hash)));
type Entries = [string, number][];
const hashmap: Entries[] = _.times(256, () => []);
steps.forEach((step) => {
if (step.at(step.length - 1) === "-") {
const label = step.slice(0, -1);
const key = hash(label);
_.remove(hashmap[key], (pair) => pair[0] === label);
} else {
const [label, focalLength] = step.split("=");
const key = hash(label);
const target = _.find(hashmap[key], (pair) => pair[0] === label);
if (target) {
target[1] = parseInt(focalLength);
} else {
hashmap[key].push([label, parseInt(focalLength)]);
}
}
});
// Part 2
console.log(
_.sum(
hashmap.map((entries, key) =>
_.sum(
entries.map(
([_label, focalLength], slotIndex) =>
(key + 1) * (slotIndex + 1) * focalLength
)
)
)
)
);