-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
110 lines (95 loc) · 2.44 KB
/
index.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import run from "aocrunner";
import "../utils/index.js";
const parseInput = (rawInput: string) => rawInput.replace("\n", "").split(",");
const hash = (input: string) => {
let currVal = 0;
for (const char of input) {
currVal += char.charCodeAt(0);
currVal = currVal * 17;
currVal = currVal % 256;
}
return currVal;
}
const part1 = (rawInput: string) => {
const input = parseInput(rawInput);
return input.reduce((valueSum, step) => {
return valueSum + hash(step);
}, 0);
};
type Lens = {
label: string;
focalLength: number;
};
const part2 = (rawInput: string) => {
const input = parseInput(rawInput);
const boxes = new Map<number, Lens[]>();
input.forEach(instruction => {
const matches = instruction.match(/(\w+)([=\-])(\d+)?/)!!;
const label = matches[1];
const boxIndex = hash(label);
const operation = matches[2];
// console.log(`label: ${label}, index: ${boxIndex}, operation: ${operation}`);
if (operation === "=") {
let box = boxes.get(boxIndex);
if (!box) {
box = [];
boxes.set(boxIndex, box);
}
const focalLength = Number(matches[3]);
const existingLens = box.find(lens => lens.label === label);
if (existingLens) {
existingLens.focalLength = focalLength;
} else {
box.push({
label,
focalLength
});
}
} else if (operation === "-") {
const box = boxes.get(boxIndex);
if (!box) {
boxes.set(boxIndex, []);
return;
}
const indexOfLensInBox = box.findIndex(lens => lens.label === label);
if (indexOfLensInBox !== -1) {
box.splice(indexOfLensInBox, 1);
}
} else {
throw Error("Heehoo peenut");
}
});
// console.log(boxes);
return Array.from(boxes.entries()).reduce((accum, [index, lenses]) => {
const boxVal = 1 + index;
return accum + lenses.reduce((lensTotal, lens, index) => {
return lensTotal + boxVal * (index + 1) * (lens.focalLength);
}, 0);
}, 0);
};
run({
part1: {
tests: [
{
input: `HASH`,
expected: 52,
},
{
input: `rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7`,
expected: 1320,
},
],
solution: part1,
},
part2: {
tests: [
{
input: `rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7`,
expected: 145,
},
],
solution: part2,
},
trimTestInputs: true,
onlyTests: false,
});