
The Problem
Imagine a circular dial (like a combination lock) with 100 positions numbered 0 to 99. The dial starts pointing at 50.
You receive a sequence of rotation instructions:
L= rotate Left (toward lower numbers)R= rotate Right (toward higher numbers)The number following indicates how many "clicks" to rotate
Because the dial is circular:
Going left from 0 wraps to 99
Going right from 99 wraps to 0
Summary
Part | Question | Strategy |
|---|---|---|
1 | How many times do we end on 0? | Simple: check |
2 | How many times do we pass through 0? | Calculate distance to first 0, then count full rotations |
Part 1: Count Final Positions at Zero
Goal: Count how many times the dial lands on 0 at the end of a rotation.
The Modulo Wrap-Around Formula
This formula handles circular arithmetic:
dial + sign * distance→ Calculate the raw new positionsignis-1for Left,+1for Right
% range→ First modulo brings it into range, but can be negative!Example:
(5 - 10) % 100 = -5❌
+ range→ Add 100 to handle negative results-5 + 100 = 95✓
% range→ Final modulo ensures we're in[0, 99]If the first result was positive, this cancels the extra
+ range
Why this works: In JavaScript, -5 % 100 returns -5, not 95. The double-modulo trick ensures we always get a positive result in the valid range.
Part 2: Count Every Click Through Zero
Goal: Count every time the dial passes through 0, not just when it lands there.
This is the tricky part! If you're at position 50 and rotate L200, you'll pass through 0 twice.
The Key Insight: Calculate Distance to First Zero
Current Position | Direction | Distance to First Zero |
|---|---|---|
| Left ( |
|
| Right ( |
|
| Either |
|
Visual example going Left from 50:
Visual example going Right from 50:
Counting All Zero Crossings
The logic:
Check if we even reach zero:
distance >= firstZeroIf we don't travel far enough, we never hit zero
Count the crossings:
+1→ We definitely hit zero at least once (atfirstZero)(distance - firstZero) / range→ After the first zero, how many complete 100-click rotations fit?Each complete rotation = one more pass through zero
Example: Position 50, instruction L250
firstZero = 50(distance to reach 0 going left)distance = 250≥50✓ we will hit zeroRemaining after first zero:
250 - 50 = 200Complete rotations:
Math.floor(200 / 100) = 2Total zeros:
2 + 1 = 3🎯