-
Notifications
You must be signed in to change notification settings - Fork 0
/
day15.js
90 lines (77 loc) · 2.17 KB
/
day15.js
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
import { read, readEx, time } from './util.js';
import Vec from './util/Vec.js';
await import('./util/bad-but-great.js');
await readEx();
await read();
let data;
// 2:37 -- RANK 212 (So close to leaderboard!)
const partOne = () => {
const hash = (str) => {
let sum = 0;
str.split``.forEach((c) => {
sum += c.charCodeAt(0);
sum *= 17;
sum %= 256
})
return sum;
}
let steps = data.split(',').map(hash).sum();
//console.log(hash('HASH'));
console.log(steps);
};
// 20:56 -- I'm really bad at reading lmao
const partTwo = () => {
const hash = (str) => {
let sum = 0;
str.split``.forEach((c) => {
sum += c.charCodeAt(0);
sum *= 17;
sum %= 256
})
return sum;
}
let table = Array(256).fill(null).map(() => []);
for (const step of data.split(',')) {
if (step.includes('=')) {
const [ key, val ] = step.split('=');
let h = hash(key);
let added = false;
for (const i in table[h]) {
const [ek,] = table[h][i];
if (ek === key) {
table[h][i][1] = +val;
added = true;
break;
}
}
if (!added) {
table[h].push([key, +val]);
}
} else {
const [ key ] = step.split('-');
let h = hash(key);
table[h] = table[h].filter(([k,]) => k !== key);
}
// console.log({ step });
// console.log('table:');
// for (const i in table) {
// const row = table[+i];
// if (row.length) {
// console.log(+i, row);
// }
// }
}
let sum = table.map((l, i) => l.map(([, v], j) => v * (i + 1) * (j + 1)).sum()).sum();
console.log({ sum});
};
if (process.argv[2]) {
console.log('--- --- Running Sample Data --- ---');
data = await readEx(); // Sample Data
time(partOne);
time(partTwo);
} else {
console.log('--- --- Running Real Data --- ---');
data = await read(); // Real Data
time(partOne);
time(partTwo);
}