main.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. Array.prototype.count = function(fnc) {
  2. return this.reduce((total, i, index) => fnc(i, index) ? total+1 : total, 0);
  3. }
  4. Array.prototype.countUnique = function(fnc) {
  5. return this.reduce((total, i, index) => (total.indexOf(i) === -1 && fnc(i, index)) ? total.concat(i):total, []).length;
  6. }
  7. function reOrder(arr, index) {
  8. let item = arr.splice(index, 1)[0];
  9. let newPos = (item.val + index) % (arr.length);
  10. while (newPos <= 0)
  11. newPos += arr.length;
  12. arr.splice(newPos, 0, item);
  13. }
  14. (async()=>{
  15. let dataP1 = [];
  16. let dataP2 = [];
  17. let index = 0;
  18. for await (let line of require('readline').createInterface({ input: process.stdin })) {
  19. dataP1.push({ val: parseInt(line), index: index });
  20. dataP2.push({ val: parseInt(line), index: index });
  21. ++index;
  22. }
  23. for (let i =0; i < dataP1.length; ++i) {
  24. reOrder(dataP1, dataP1.findIndex(j => j.index === i));
  25. dataP2[i].initVal = dataP2[i].val * 811589153;
  26. dataP2[i].val *= 811589153;
  27. }
  28. const pos0 = dataP1.findIndex(i => i.val === 0);
  29. console.log("Part 1:", [1000, 2000, 3000].map(i => dataP1[(pos0 +i) % dataP2.length].val).reduce((acc, i) => acc+i, 0));
  30. for (let _c =0; _c < 10; ++_c)
  31. for (let i =0; i < dataP2.length; ++i)
  32. reOrder(dataP2, dataP2.findIndex(j => j.index === i));
  33. const pos0Part2 = dataP2.findIndex(i => i.val === 0);
  34. console.log("Part 2:", [1000, 2000, 3000].map(i => dataP2[(pos0Part2+i) % dataP2.length].initVal).reduce((acc, i) => acc+i, 0));
  35. })();