Skip to content

Instantly share code, notes, and snippets.

@sprzybylski
Last active December 1, 2025 07:07
Show Gist options
  • Select an option

  • Save sprzybylski/4000eb0889e50102271dfe6d822d6c37 to your computer and use it in GitHub Desktop.

Select an option

Save sprzybylski/4000eb0889e50102271dfe6d822d6c37 to your computer and use it in GitHub Desktop.
const input = ["L68", "L30", "R48", "L5", "R60", "L55", "L1", "L99", "R14", "L82"];
let pointer = 50;
const result = input
.map((instruction) => {
const direction = instruction.charAt(0);
const distance = parseInt(instruction.slice(1), 10);
return { direction, distance };
})
.reduce((acc, { direction, distance }) => {
const toMove = distance % 100;
let zerosCounted = (distance - toMove) / 100;
switch (direction) {
case "L":
zerosCounted += pointer - toMove < 0 ? 1 : 0;
tmp = pointer - toMove < 0 ? Math.abs(pointer - toMove) : pointer - toMove;
break;
case "R":
zerosCounted += pointer + toMove > 100 ? 1 : 0;
tmp = (pointer + toMove) % 100;
break;
}
return acc + zerosCounted;
}, 0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment