-
Notifications
You must be signed in to change notification settings - Fork 0
/
20.js
58 lines (48 loc) · 1.1 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
const { sum } = require("lodash");
const isTesting = false;
const input = isTesting
? `1
2
-3
3
-2
0
4`
: ``;
const coords = input.trim().split("\n").map(Number);
const nextPosition = (position, nCoords) => {
let res = position % (nCoords - 1);
if (res <= 0) res += nCoords - 1;
return res;
};
const getScore = (arr) => {
// console.log(arr.map((e) => e.value).join(", "));
const zeroPos = arr.findIndex((r) => r.value === 0);
return sum(
[1000, 2000, 3000].map((v) => arr[(v + zeroPos) % arr.length].value)
);
};
const mix = (values, nReps) => {
const result = values.map((value, originalIdx) => ({
value,
originalIdx,
}));
for (let rep = 0; rep < nReps; rep++) {
values.forEach((_, i) => {
const nextIdx = result.findIndex((e) => e.originalIdx === i);
const next = result.splice(nextIdx, 1)[0];
result.splice(nextPosition(nextIdx + next.value, values.length), 0, next);
});
}
return result;
};
console.log("Part 1", getScore(mix(coords, 1)));
console.log(
"Part 2",
getScore(
mix(
coords.map((v) => v * 811589153),
10
)
)
);