-
Notifications
You must be signed in to change notification settings - Fork 0
/
day9.js
77 lines (73 loc) · 1.79 KB
/
day9.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
require('./newStdLib');
const advent = require('./advent');
const tests = [[1, 48,95], [9, 24, 32], [9,48,63], [10, 1618, 8317], [13, 7999, 146373], [17, 1104 ,2764], [21, 6111, 54718], [30, 5807, 37305]];
const input = [[491, 71058], [491, 7105800]];
function part1(players, marbles) {
var struct = [0],
curr = 1,
index = 0,
player = 0
scores = Array(players).fill(0);
while (marbles--) {
if (!(curr % 23)) {
scores[player] += parseInt(curr);
if (index < 7) {
index = index - 7 + struct.length
} else {
index = index - 7 % struct.length
}
scores[player] += struct.splice(index, 1)[0];
} else {
var prev = index;
if (struct.length > 1) {
index = (index + 2) % struct.length;
} else {
index++;
}
if (index == 0) {
struct.push(curr);
index = struct.length - 1;
} else {
struct.splice(index, 0, curr)
}
}
curr++;
player = (player + 1) % players;
}
return scores.sort((a, b) => b-a)[0];
}
function part2(players, marbles) {
var currNode = {v: 0, p: null, n: null};
currNode.p = currNode;
currNode.n = currNode;
curr = 1,
index = 0,
player = 0
scores = Array(players).fill(0),
first = currNode;
while (curr <= marbles) {
if (!(curr % 23)) {
scores[player] += curr;
for (var i = 0; i < 7; i++) {
currNode = currNode.p;
}
scores[player] += currNode.v;
currNode.p.n = currNode.n;
currNode.n.p = currNode.p;
currNode = currNode.n;
} else {
currNode = currNode.n
var newNode = {v: curr, p: currNode, n: currNode.n};
currNode.n.p = newNode;
currNode.n = newNode;
currNode = newNode;
}
curr++;
player = (player + 1) % players;
}
return scores.sort((a, b) => b-a)[0];
}
advent.tests(part1, tests);
advent.runs(part1, input[0]);
advent.tests(part2, tests);
advent.runs(part2, input[1]);