day 20 aoc: the cupid shuffle

https://youtube.com/watch?v=h24_zoqu4_Q

to the right to the right to the right

to the left to the left to the left

post your aoc solutions for day 20 or smth, all checks notes 5+ of you

42
Jump in the discussion.

No email address required.

// cause js modulus is stupid
const mod = (n: number, m: number) => ((n % m) + m) % m;

const part = Number(process.argv[3]);
const input = fs.readFileSync(process.argv[2], 'utf-8').trim().split('\n').map(Number);
let nums = input.map((n,i)=>({i,n: part == 2 ? n*811589153 : n}));
const len = nums.length;

for (let i=0; i < (part === 2 ? 10 : 1); i++)
  input.map((_,i) => {
    const oldI = nums.findIndex(num => num.i === i);
    const num = nums[oldI];
    
    // normalize large number by len-1 (exclude jumping over oneself)
    const diff = mod(num.n, (len-1));
    // wrap new index by len-1 (also exclude jumping over oneself)
    const newI = mod(oldI + diff, len-1);
    
    nums
      = oldI > newI ? [nums.slice(undefined,newI), num, nums.slice(newI,oldI), nums.slice(oldI+1)].flat()
      : oldI < newI ? [nums.slice(0,oldI), nums.slice(oldI+1,newI+1), num, nums.slice(newI+1)].flat()
      : nums 
  });

const idx0 = nums.findIndex(n => n.n === 0)
const ans = [1000,2000,3000]
  .reduce((p,i) => p + nums[(idx0+i)%len].n, 0);

console.log({ans});
Jump in the discussion.

No email address required.

Link copied to clipboard
Action successful!
Error, please refresh the page and try again.