-
Notifications
You must be signed in to change notification settings - Fork 0
/
day23-1.js
215 lines (191 loc) · 4.65 KB
/
day23-1.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
const { readData } = require("./readfile");
const _ = require("lodash");
// const input = readData('day23Input.txt');
// const array = "389125467".split("").map((x) => parseInt(x));
const array = "284573961".split("").map((x) => parseInt(x));
const debug = false;
const log = (param) => {
if (debug) {
console.log(param);
}
};
const arrayToLinkedList = (array) => {
if (array.length === 0) {
return {};
}
const head = {
value: array[0],
};
if (array.length === 1) {
return head;
}
let prev = head;
for (let i = 1; i < array.length; i++) {
const node = {
value: array[i],
prev: prev,
};
prev.next = node;
prev = node;
}
prev.next = head;
head.prev = prev;
return head;
};
const printNode = (node) => {
log(node.value);
};
const buildMapForLinkedList = (linkedList) => {
const map = new Map();
walk(linkedList, (node) => map.set(node.value, node));
return map;
};
const printMap = (map) => {
for (let value of map.values()) {
printNode(value);
}
};
const walk = (linkedList, fun) => {
const head = linkedList;
let current = linkedList.next;
fun(head);
while (current && current !== head) {
fun(current);
current = current.next;
}
};
/**
* current -> destination
* destination -> toMoveHead
* toMoveEnd -> following
*
*/
const moveIn = (current, toMoveHead, toMoveTail) => {
toMoveTail.next = current.next;
current.next = toMoveHead;
toMoveHead.prev = current;
toMoveTail.next.prev = toMoveTail;
return current;
};
const moveOut = (current, following) => {
current.next = following;
following.prev = current;
return current;
};
const move3Nodes = (current, destination) => {
const toMoveHead = current.next;
const toMoveTail = toMoveHead.next.next;
const following = toMoveTail.next;
moveOut(current, following);
moveIn(destination, toMoveHead, toMoveTail);
return current.next;
};
/**
* smallestFour: 5,4,3,2
* largestFour: 6,7,8,9
*/
const findDestination = (
current,
toMoveValueList,
map,
largestFour,
smallestFour
) => {
let value = current.value - 1;
// log(`toMoveValueList: ${toMoveValueList}`);
while (true) {
if (toMoveValueList.includes(value)) {
value -= 1;
continue;
}
if (value < smallestFour[0]) {
value = largestFour[0];
continue;
}
break;
}
return map.get(value);
};
const findToMoveValueList = (current) => {
return [
current.next.value,
current.next.next.value,
current.next.next.next.value,
];
};
const getValuesAfter1 = (map) => {
const node1 = map.get(1);
const array = [];
walk(node1, (x) => array.push(x.value));
return array.join("").slice(1, array.length);
};
const run = (current, map, largestFour, smallestFour) => {
const toMoveValueList = findToMoveValueList(current);
const destination = findDestination(
current,
toMoveValueList,
map,
largestFour,
smallestFour
);
log(
`toMoveValueList: ${toMoveValueList}, destinationValue: ${destination.value}`
);
const next = move3Nodes(current, destination);
return next;
};
const run100 = (array) => {
let sorted = [...array].sort();
let largestFour = sorted.slice(sorted.length - 4).reverse();
let smallestFour = sorted.slice(0, 4);
const linkedList = arrayToLinkedList(array);
// walk(linkedList, printNode);
// log("-----");
const map = buildMapForLinkedList(linkedList);
// printMap(map);
// log("-----");
let i = 0;
let current = linkedList;
while (i < 100) {
current = run(current, map, largestFour, smallestFour);
walk(current, printNode);
i++;
// log("-----");
}
let result = getValuesAfter1(map);
console.log(result);
};
const generateNumberArray = (start, end) =>
Array(end - start + 1)
.fill()
.map((_, i) => i + start);
const run1000000 = (array) => {
let sorted = [...array].sort();
let following = generateNumberArray(10, 1000000);
let largestFour = following.slice(sorted.length - 4).reverse();
let smallestFour = sorted.slice(0, 4);
const linkedList = arrayToLinkedList([...array, ...following]);
// walk(linkedList, printNode);
// log("-----");
const map = buildMapForLinkedList(linkedList);
// printMap(map);
// log("-----");
let i = 0;
let current = linkedList;
while (i < 10000000) {
current = run(current, map, largestFour, smallestFour);
// walk(current, printNode);
i++;
// log("-----");
if (i % 1000000 === 0) {
console.log(`current: ${i}`);
}
}
current = map.get(1);
const next = current.next.value;
const nextNext = current.next.next.value;
console.log(next, nextNext, next * nextNext);
};
// console.log(array);
// run100(array);
run1000000(array);