-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday15.ts
77 lines (63 loc) · 2 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { assertEquals } from "https://deno.land/std@0.208.0/assert/mod.ts";
import { loadTestData, loadData } from "./utils.ts";
const part1test = 1320;
const part2test = 145;
interface Lens {
label: string;
focalLength: number;
}
const hash = (str: string): number => {
let curr = 0;
while (str.length > 0) {
curr += str.charCodeAt(0);
str = str.substring(1);
curr *= 17;
curr %= 256;
}
return curr;
}
const part1 = (data: string) => {
const parts = data.split(",");
const hashed = parts.map(hash);
return hashed.reduce((a, b) => a + b);
};
const part2 = (data: string) => {
const parts = data.split(",");
const boxes: Lens[][] = new Array(256).fill(null).map(() => []);
for (const part of parts) {
let label: string;
if (part.includes('=')) {
label = part.split("=")[0];
} else {
label = part.substring(0, part.length - 1);
}
const hashed = hash(label);
if (part.includes('=')) {
const lens = boxes[hashed].filter(l => l.label == label)[0];
const focalLength = parseInt(part.split('=')[1]);
if (lens) {
lens.focalLength = focalLength;
} else {
boxes[hashed].push({
label,
focalLength,
});
}
} else {
boxes[hashed] = boxes[hashed].filter(l => l.label != label);
}
}
let sum = 0;
for (let boxNum = 0; boxNum < boxes.length; boxNum++) {
const box = boxes[boxNum];
for (let lensNum = 0; lensNum < box.length; lensNum++) {
const focalLength = box[lensNum].focalLength;
sum += (boxNum + 1) * (lensNum + 1) * focalLength;
}
}
return sum;
};
assertEquals(part1(await loadTestData(15)), part1test)
console.log(part1(await loadData(15)))
assertEquals(part2(await loadTestData(15)), part2test)
console.log(part2(await loadData(15)))