Crane Truck (Small)
Time limit10sMemory limit512 MB
Run each crane program on 240 circular cells with wrapping crate counts and count the truck's forward and backward moves.
- Level
Easy3 of 10
- Topics
- Simulation, Implementation
- Solved
- No attempts yet
Problem
A storage yard has 240 storage locations arranged in a circle. A truck with a crane on it drives around the circle and picks up or puts down crates according to a program. The truck carries an unlimited supply of crates, so it can always put another crate down.
A program is a sequence of these instructions:
b: move back one locationf: move forward one locationu: pick up one crate at the current locationd: put down one crate at the current location(: do nothing): if the current location holds more than one crate, go back to the most recent(in the program and continue from there. The truck itself does not move.
Every ( has a matching ) later in the program. A program holds at most two such pairs, and the pairs are never nested. A program therefore has one of three shapes: no ( and no ) at all, one ( followed later by one ), or (, ), (, ) in that order.
Before the truck starts, every location holds exactly one crate. All locations look the same at that moment, so the answer does not depend on which location the truck starts at.
If the truck picks up the last crate at a location, another truck instantly arrives and puts down 256 crates there. If the truck puts a crate down and the location then holds 257 crates, another truck instantly drives past, picks up 256 of them and leaves one behind. Every location therefore always holds between 1 and 256 crates.
Count how many times the truck moves forward or backward before its program ends.
Input
The first line holds an integer , the number of programs.
Each of the next lines holds one program.
Limits
- (length of a program)
- A program uses only the characters
b,f,u,d,(and). - Every program halts.
- A program holds at most two pairs of
(and), and the pairs are never nested. - Running all programs executes at most instructions in total.
Output
Print lines. For the -th program print Case #i: Y, where is the number of times the truck moves forward or backward.