-
Notifications
You must be signed in to change notification settings - Fork 0
/
20.js
66 lines (55 loc) · 1.54 KB
/
20.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
// Total duration: 19 min 40 seconds (would have been 83rd on the leaderboard)
// Started at: 19:29
// First star at: 19:46
// Second star at: 19:48:20
const solveProblem = (data) => {
// Parse data
const lines = data.split("\n").slice(0, -1);
let values = lines.map((line, i) => ({i, value: Number(line)}));
// Part 1
const mixValue = (i) => {
const index = values.findIndex(v => v.i == i)
const [value] = values.splice(index, 1);
let newIndex = (index + value.value + values.length) % values.length;
// console.log({value, newIndex})
// console.log(values);
values.splice(newIndex, -1, value);
}
for (let i = 0; i < values.length; i++) {
mixValue(i);
}
const computeResult = () => {
const zeroIndex = values.findIndex(v => v.value == 0);
return (
values[(zeroIndex + 1000) % values.length].value +
values[(zeroIndex + 2000) % values.length].value +
values[(zeroIndex + 3000) % values.length].value
);
}
console.log(computeResult());
console.log("");
// Part 2
const key = 811589153;
values = lines.map((line, i) => ({i, value: Number(line) * key}));
for (let k = 0; k < 10; k++) {
for (let i = 0; i < values.length; i++) {
mixValue(i);
}
}
console.log(computeResult());
console.log("");
};
// Library code
const fs = require("fs");
const path = require('path');
const day = path.parse(__filename).name;
const files = [
`${day}-test`,
`${day}`,
];
files.forEach(file => {
fs.readFile(`./${file}.txt`, "utf8", (_, data) => {
console.log(`Data from file ${file}.txt`);
solveProblem(data);
});
});