|
|
@@ -0,0 +1,44 @@
|
|
|
+
|
|
|
+Array.prototype.count = function(fnc) {
|
|
|
+ return this.reduce((total, i, index) => fnc(i, index) ? total+1 : total, 0);
|
|
|
+}
|
|
|
+
|
|
|
+Array.prototype.countUnique = function(fnc) {
|
|
|
+ return this.reduce((total, i, index) => (total.indexOf(i) === -1 && fnc(i, index)) ? total.concat(i):total, []).length;
|
|
|
+}
|
|
|
+
|
|
|
+function reOrder(arr, index) {
|
|
|
+ let item = arr.splice(index, 1)[0];
|
|
|
+ let newPos = (item.val + index) % (arr.length);
|
|
|
+ while (newPos <= 0)
|
|
|
+ newPos += arr.length;
|
|
|
+ arr.splice(newPos, 0, item);
|
|
|
+}
|
|
|
+
|
|
|
+(async()=>{
|
|
|
+ let dataP1 = [];
|
|
|
+ let dataP2 = [];
|
|
|
+ let index = 0;
|
|
|
+ for await (let line of require('readline').createInterface({ input: process.stdin })) {
|
|
|
+ dataP1.push({ val: parseInt(line), index: index });
|
|
|
+ dataP2.push({ val: parseInt(line), index: index });
|
|
|
+ ++index;
|
|
|
+ }
|
|
|
+ for (let i =0; i < dataP1.length; ++i) {
|
|
|
+ reOrder(dataP1, dataP1.findIndex(j => j.index === i));
|
|
|
+ dataP2[i].initVal = dataP2[i].val * 811589153;
|
|
|
+ dataP2[i].val *= 811589153;
|
|
|
+ }
|
|
|
+ const pos0 = dataP1.findIndex(i => i.val === 0);
|
|
|
+ console.log("Part 1:", [1000, 2000, 3000].map(i => dataP1[(pos0 +i) % dataP2.length].val).reduce((acc, i) => acc+i, 0));
|
|
|
+
|
|
|
+ for (let _c =0; _c < 10; ++_c)
|
|
|
+ for (let i =0; i < dataP2.length; ++i)
|
|
|
+ reOrder(dataP2, dataP2.findIndex(j => j.index === i));
|
|
|
+ const pos0Part2 = dataP2.findIndex(i => i.val === 0);
|
|
|
+ console.log("Part 2:", [1000, 2000, 3000].map(i => dataP2[(pos0Part2+i) % dataP2.length].initVal).reduce((acc, i) => acc+i, 0));
|
|
|
+})();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|