We discuss a Google code jam problem
A large storage facility has 240 storage locations arranged in a circle.
A truck with a crane on it moves along the circle of storage locations, picking up or putting down crates according to a program. (The truck has an unlimited supply of crates on board, so it can always put more crates down.)
The program consists of 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 there is more than one crate at the current location, move back to the most recent ( in the sequence of instructions, and continue the program from there. (This doesn't move the truck.)
- no ( or ) instructions;
- one ( instruction somewhere in the program, followed later by one ) instruction;
- a ( instruction, followed later by a ) instruction, followed later by another (, and again later by another ).
- There are always between 1 and 256 crates at any location.
How many movements will the truck make before reaching the end of the program.
For example :
Input :
ufffdddbbbdd has three forward and three backward movements.
ddd (fdbu)fff had two movements repeated one for instruction in the enclosed sequence and followed by three forward motions totalling 11
dddd (fdddddbu)f (fddddddu) has sixteen motions due to the pattern of the enclosed sequence with the motions from the second sequence repeated twice due to the single forward instruction just prior to the sequence leading to a total of 3×16+1=48
Int getMotions (string sequence)
{
Int total = 0;
Int I = 0;
While( I < sequence.Count)
{
I++;
If (sequence[i] == 'f' || sequence [i] == 'b') {total++; }
If (sequence[i] == '(')
{
Int count = 0;
Int sum = 0;
For ( int j = i+1; j < sequence.Count && sequence [j] != ')'; j++)
{
count += 1;
if (sequence[j] == 'f' || sequence [j] == 'b') sum++;
assert (sequence[j] == '(' || sequence [j] == ')');
}
sum *= count;
If ( i > 1 && ( sequence[i-1] == 'f' || sequence [i-1] == 'b'))
sum *= 2;
total += sum;
i += count;
}
}
Return total;
}
For example :
Input :
ufffdddbbbdd has three forward and three backward movements.
ddd (fdbu)fff had two movements repeated one for instruction in the enclosed sequence and followed by three forward motions totalling 11
dddd (fdddddbu)f (fddddddu) has sixteen motions due to the pattern of the enclosed sequence with the motions from the second sequence repeated twice due to the single forward instruction just prior to the sequence leading to a total of 3×16+1=48
Int getMotions (string sequence)
{
Int total = 0;
Int I = 0;
While( I < sequence.Count)
{
I++;
If (sequence[i] == 'f' || sequence [i] == 'b') {total++; }
If (sequence[i] == '(')
{
Int count = 0;
Int sum = 0;
For ( int j = i+1; j < sequence.Count && sequence [j] != ')'; j++)
{
count += 1;
if (sequence[j] == 'f' || sequence [j] == 'b') sum++;
assert (sequence[j] == '(' || sequence [j] == ')');
}
sum *= count;
If ( i > 1 && ( sequence[i-1] == 'f' || sequence [i-1] == 'b'))
sum *= 2;
total += sum;
i += count;
}
}
Return total;
}
No comments:
Post a Comment