Simulate Players Moving Under a Watcher's Gaze
A one-dimensional road starts at 0 and ends at length. Each player begins at a supplied position and wants to move right at one unit per second until reaching length. A watcher begins at watcher_start, initially faces left, and moves one unit per second in the direction faced. At specified integer timestamps, the watcher reverses direction.
Use this discrete-time model for each second t = 0, 1, ..., time_limit - 1:
-
If
t
is a flip timestamp, reverse the watcher's direction before observing players.
-
A player is observed when the player is strictly in front of the watcher in the direction the watcher faces. A player at the watcher's exact position is also observed.
-
Observed players do not move during that second. Every unobserved player that has not finished moves one unit right, capped at
length
.
-
The watcher moves one unit in its current direction. The watcher is not clamped to the road endpoints.
Implement:
count_finishers(time_limit, player_starts, watcher_start, length, flip_times) -> int
Return how many players have reached length by the end of time_limit seconds.
Example
time_limit = 2
player_starts = [1, 4]
watcher_start = 3
length = 5
flip_times = [1]
result = 1
During second 0 the watcher faces left, so the player at 1 is frozen while the player at 4 reaches 5. At timestamp 1 the watcher reverses before the second observation. Only one player has finished by time 2.
Constraints
-
0 <= time_limit <= 100000
-
0 <= len(player_starts) <= 100000
-
0 <= player_start <= length
-
length >= 0
-
Flip timestamps are unique integers in
[0, time_limit - 1]
and may be unsorted.
-
A player already at
length
counts as finished and no longer moves.
Clarifications
-
All observations use positions at the start of the second, after any flip.
-
Player and watcher movement then occur conceptually simultaneously.
-
Multiple players may occupy the same position.
Hints
-
A direct simulation is adequate only when the product of players and seconds is small.
-
First derive the watcher's piecewise-linear position from sorted flip events.
-
Consider how the observed half-line changes over time before choosing an optimization.
Discussion Extensions
-
Give a straightforward solution and its complexity.
-
How could event intervals be used to avoid checking every player every second?
-
How would the model change if the watcher were clamped to the road?