← all problems

pingPongPosition 🌶️🌶️

A scanner light sweeps back and forth along a strip of lights numbered 0 to len. It starts on light 0 and moves one light every tick, heading up. When it reaches light len it turns around and heads down, and when it gets back to light 0 it turns around and heads up again.

Write a method pingPongPosition that takes len (1 or more) and the number of ticks (0 or more) and returns which light it is on after that many ticks.

For example, with len 3 the light visits 0, 1, 2, 3, 2, 1, 0, 1, ... so pingPongPosition(3, 4) is 2.