You are given a text file that describes batches of register operations (reads and writes) on integer-indexed registers. Your task is to schedule and execute these operations using provided functions, while respecting timing constraints and (for Level 2) intra-batch dependencies.
You may reorder operations within a batch, but must obey all constraints described below.
Implement the function:
def execute_operations_from_file(file_path: str):
...
You are provided with the following primitives (assume they are already implemented):
def read_reg(register_index: int) -> int:
"""Return the current value of the register with the given index."""
...
def write_reg(register_index: int, value: int) -> None:
"""Write `value` into the register with the given index."""
...
Your implementation of execute_operations_from_file must read the file, compute a valid schedule, and invoke read_reg / write_reg in that order.
Use a logical time measured in milliseconds (ms). You do not need to wait in real time; just ensure your computed schedule respects these rules:
t
, the next operation may start at time
t + 1
or later.
r
starts at time
t
, no
read
of register
r
may start until time
t + 10
or later.
r
and any subsequent read of
r
, there must be at least 10 ms.
Your goal is to produce a valid schedule that respects all of the above rules. Within a batch, you may choose any ordering that leads to a valid schedule. (You may try to minimize the total completion time of all operations, but correctness—i.e., respecting constraints—is the primary requirement.)
The input is a plain text file. Each non-empty line is one of the following forms:
<reg_index> w <value>
<reg_index>
: an integer register index (e.g.,
0
,
1
,
2
, ...).
w
: the literal character
w
(write).
<value>
: an integer value to be written.
2 w 100
means:
write the value 100 to register 2
.
<reg_index> r
<reg_index>
: an integer register index.
r
: the literal character
r
(read).
5 r
means:
read from register 5
.
p
or
P
(case-insensitive: treat
p
and
P
the same).
Each
p
line
ends the current batch
and starts a
new batch
. The file therefore consists of one or more batches of operations, separated by
p
lines.
k+1 may start before all operations in batch k have finished
, according to the logical time schedule.
<reg_index>
.
For Level 2, some read operations within a batch may have an additional dependency on another operation line within the same batch.
Such a dependent read operation has the format:
<reg_index> r <dependent_line_number>
where:
<reg_index>
: an integer register index to be read.
r
: the literal character
r
.
<dependent_line_number>
: a
1-based line index within the same batch
, referring to another operation
line
in that batch.
p
/
P
lines.
<dependent_line_number>
is valid and refers to a line in the same batch as this read.
Dependency rule:
A read operation written as <reg_index> r <dependent_line_number> must not be scheduled before the operation on the referenced line has executed. In other words, for that pair of operations, your schedule must respect the given precedence constraint in addition to all global timing constraints.
All previous rules (single operation at a time, 1 ms gap, 10 ms read-after-write, batch ordering) still apply.
Example input file (showing multiple batches):
2 w 100
3 w 10
4 w 10
5 r
p
4 r
1 r
2 r
p
3 w 10
p
2 w 10
3 w 8
5 r
This file describes multiple batches of reads and writes, separated by lines containing p.
execute_operations_from_filefile_path
.
p
/
P
lines as separators.
k
finish before any operation in batch
k+1
begins.
write_reg
and
read_reg
according to the order implied by your schedule.
execute_operations_from_file
does not need to return any value; its effect is the sequence of calls to
read_reg
and
write_reg
executed in an order that satisfies all constraints above.