-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
178 lines (151 loc) · 3.24 KB
/
index.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import run from "aocrunner";
const parseInput = (rawInput) => rawInput.split('\n').map(e=>e*1);
class LinkedList{
constructor(input){
this.length = input.length;
this.head = null;
this.zero = null;
this.ordered = [];
let lastNode = null;
for (const line of input) {
let index = 0;
const node = new Node(index, line);
this.ordered.push(node);
if (this.head === null) {
node.isHead = true;
this.head = node;
}
if (lastNode) {
lastNode.next = node;
node.prev = lastNode;
}
if (line === 0) {
this.zero = node;
}
lastNode = node;
}
lastNode.next = this.head;
this.head.prev = lastNode;
}
decrypt() {
for (const node of this.ordered) {
if (node.value === 0) continue;
const modValue = node.value % (this.length - 1);
if (modValue === 0) continue;
const { next, prev } = node;
if (node.isHead) {
this.head = next;
node.isHead = false;
next.isHead = true;
}
prev.next = next;
next.prev = prev;
let insertAfter = node;
for (let i = 0; i < modValue; i++) {
insertAfter = insertAfter.next;
}
for (let i = 0; i >= modValue; i--) {
insertAfter = insertAfter.prev;
}
const insertBefore = insertAfter.next;
insertAfter.next = node;
insertBefore.prev = node;
node.next = insertBefore;
node.prev = insertAfter;
// print(this);
}
}
getNthItem(nth) {
const offset = nth % this.length;
let node = this.zero;
for (let i = 0; i < offset; i++) {
node = node.next;
}
return node.value;
}
}
class Node{
constructor(inputIndex, value){
this.inputIndex = inputIndex;
this.value = value;
}
next = null;
prev = null;
isHead = false;
}
function print(linkedList) {
let node;
let result = [];
for (let i = 0; i < linkedList.length; i++) {
if (!node) {
node = linkedList.head;
} else {
node = node.next;
}
result.push(node.value);
}
console.log(result.slice(0, 10))
}
const part1 = (rawInput) => {
const input = parseInput(rawInput);
const linkedList = new LinkedList(input);
linkedList.decrypt();
// print(linkedList);
let total = 0;
for (let i = 1; i <= 3; i++) {
total += linkedList.getNthItem(1000 * i);
}
return total;
};
const part2 = (rawInput) => {
const input = parseInput(rawInput);
const decryptionKey = 811589153;
const input2 = input.map(e=>e*decryptionKey);
const linkedList = new LinkedList(input2);
for (let i = 0; i < 10; i++) {
linkedList.decrypt();
}
let total = 0;
for (let i = 1; i <= 3; i++) {
total += linkedList.getNthItem(1000 * i);
}
return total;
};
run({
part1: {
tests: [
{
input: `
1
2
-3
3
-2
0
4
`,
expected: 3,
},
],
solution: part1,
},
part2: {
tests: [
{
input: `
1
2
-3
3
-2
0
4
`,
expected: 1623178306,
},
],
solution: part2,
},
trimTestInputs: true,
// onlyTests: true,
});