The Magic Word
Time limit1sMemory limit256 MB
Interpret a spell of up to 200 lines with variables, arrays, jumps, and prints, and reproduce its output.
- Level
Medium5 of 10
- Topics
- Simulation, Implementation
- Solved
- No attempts yet
Problem
Do not meddle in the affairs of wizards, for they are subtle and quick to anger.
Gildor
Gandalf works with the arcane arts every day in his fight against Sauron. An arrangement of runes can have consequences nobody planned, so he has asked you, one of his followers, to build a system that tells him the outcome of a set of runes before he casts them.
A spell is a series of instructions, one per line. The instructions are written in elvish. Gandalf has written down what they mean.
A few notes.
- [var] takes one of three forms.
- An integer constant, for example 6. Assigning to an integer constant does nothing, and execution moves on to the next instruction.
- A variable declared with tangado, for example shadowfax. Treat it the way an int behaves in C or Java.
- An entry of an array declared with ost, for example moria[4]. The index is itself a [var], so indices nest.
- [string] is a quote-delimited string constant. It holds no quote character and no newline.
- [lbl] is a label. A label may share its name with a variable, and the two are unrelated. Labels are not declared ahead of time, so it helps to scan the whole spell for labels before you interpret it.
- After arth, the next instruction interpreted is the one right after the matching sad. An arth may point at a sad that appears later in the spell.
- sad changes no variable. It only marks a destination for arth, pen, and muindor.
- ost sets every cell of the new array to 0.
- Variable names and label names consist of uppercase and lowercase letters only.
- Integer division truncates toward zero, so 7 / 2 is 3 and -7 / 2 is -3.
- Running past the last instruction ends the spell, exactly as awarthad does.
Constraints:
- The spell holds at most 200 instruction lines.
- Every array size is between 1 and 1000.
- The spell ends after at most 100000 executed instructions.
- Every value the spell computes stays between and .
- No spell reads an undeclared variable, touches a cell outside an array, divides by zero, or jumps to a label that does not exist.
Input
The input is one spell, one instruction per line. A single line holding END follows the last instruction.
Output
Print what the spell prints: one line for every teithant and every canad it executes, in the order they run.