Runners on a Road Frozen by a Sweeping Guard
Arrays & HashingAuto-graded · 22 test casesLast updated
Simulate runners advancing along a road while a guard turns at given seconds and freezes anyone in view, using parity of duplicate turn times and strict event ordering.
Runners and the Guard
A straight road covers the integer coordinates 0, 1, ..., road_length. Several runners are on it, and the guard patrols it.
Runners. Runner i starts at coordinate starts[i]. Every second, an active runner moves exactly one unit toward higher coordinates. A runner who reaches road_length has finished and is safe for the rest of the game. A runner who has been frozen (see below) never moves again.
Guard. The guard starts at guard_pos and faces guard_dir, where 1 means facing toward higher coordinates and -1 means facing toward lower ones. The list turn_times holds the seconds at which the guard turns around. The list may be unsorted, and the same second may appear more than once. Each occurrence of a second reverses the direction once, so two occurrences in the same second cancel out.
Rounds. The game lasts steps seconds, numbered t = 1, 2, ..., steps. Nothing happens at t = 0. In each second, the following happen in this exact order:
-
Turn. Reverse the guard's direction once for every occurrence of
tinturn_times. -
Guard moves. The guard moves one unit in its current direction. If that would leave
[0, road_length], the guard stays where it is. Its facing direction is unchanged either way. -
Runners move. Every runner who is neither frozen nor finished moves one unit forward. A runner who reaches
road_lengthis finished. -
Gaze. The guard sees a range of coordinates:
- facing
1: every coordinatex >= guard position; - facing
-1: every coordinatex <= guard position.
Every runner who is not frozen, not finished (position strictly less than
road_length) and inside the seen range becomes frozen at their current coordinate. - facing
A runner who starts at road_length is already finished and is never frozen. A runner standing exactly on the guard's coordinate is seen whichever way the guard faces.
Task
Return a list with one entry per runner, in the original input order. Each entry is a pair (final_position, is_frozen), where is_frozen is True if the runner was frozen at some point and False otherwise.
Constraints
1 <= road_length <= 10^51 <= len(starts) <= 20000 <= starts[i] <= road_length0 <= guard_pos <= road_lengthguard_diris either1or-10 <= len(turn_times) <= 10^51 <= turn_times[k] <= 10^5(unsorted, duplicates allowed)1 <= steps <= 2000
Examples
Example 1
Input: simulate_runners(10, [0, 4], 5, 1, [2], 3)
Output: [[2, True], [7, False]]
Example 2
Input: simulate_runners(5, [3], 2, 1, [], 2)
Output: [[4, True]]
Hints
Hint 1
Because steps and the number of runners are both at most 2000, a direct second-by-second simulation is fast enough. The risk is in the details, not the size.
Hint 2
Do not scan turn_times every second. Count how many times each second appears once, and reverse the direction only when that count is odd.
Hint 3
Follow the four phases exactly: turn, guard move with clamping, runner move, then gaze. A runner who has just finished is never frozen, and a frozen runner never moves again.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This is a direct simulation. With at most 2000 runners and 2000 seconds, there are at most about 4 million runner-steps, which is easily fast enough.
Two details matter:
- Turn lookup.
turn_timescan hold up to 100,000 entries, so scanning it every second would be wasteful. Build a frequency table once. At secondt, the direction reverses only iftoccurs an odd number of times, because pairs of reversals cancel. Seconds beyondstepsare never looked up. - Order of events. Each second runs turn, guard move (clamped), runner move, then gaze. Nothing happens at
t = 0, so a runner who starts inside the guard's view still gets one move before the first check.
Per-runner state is a position and a frozen flag. A runner is active if they are not frozen and their position is below road_length.
Why it is correct
- A frozen runner is skipped by the movement phase, so their position never changes again.
- A finished runner sits exactly at
road_lengthand is skipped by both the movement and gaze phases, so finishing gives permanent safety. - After the turn phase at second
t, the direction equals the initial direction reversed once per occurrence of any time<= t. Counting parity per second reproduces this exactly. - The guard's facing direction is kept even when it is clamped at an end of the road. The seen range is therefore always well defined, and both comparisons are inclusive, as the rules require.
Edge cases
- A runner starting at
road_lengthis returned as(road_length, False). - A guard at
0facing-1sees only coordinate0. A guard atroad_lengthfacing1sees onlyroad_length, which holds only finished runners, so nobody is frozen. - An empty
turn_timesmeans the direction never changes.
Complexity
- Time:
O(F + steps * N), whereF = len(turn_times)andN = len(starts). - Space:
O(N + F).
Reference solution
from collections import Counter
from typing import List, Tuple
def simulate_runners(road_length: int, starts: List[int], guard_pos: int, guard_dir: int, turn_times: List[int], steps: int) -> List[Tuple[int, bool]]:
turns = Counter(turn_times)
positions = list(starts)
frozen = [False] * len(starts)
for t in range(1, steps + 1):
# 1. Turn: an odd number of occurrences reverses the direction
if turns[t] % 2:
guard_dir = -guard_dir
# 2. Guard moves, clamped to the road
guard_pos = max(0, min(road_length, guard_pos + guard_dir))
# 3. Active runners advance
for i in range(len(positions)):
if frozen[i] or positions[i] >= road_length:
continue
positions[i] += 1
# 4. Gaze: freeze unfinished, unfrozen runners in view
for i in range(len(positions)):
if frozen[i] or positions[i] >= road_length:
continue
if guard_dir == 1 and positions[i] >= guard_pos:
frozen[i] = True
elif guard_dir == -1 and positions[i] <= guard_pos:
frozen[i] = True
return [(positions[i], frozen[i]) for i in range(len(starts))]