Riddle Troll

View as PDF

Submit solution

Points: 10
Time limit: 1.0s
Memory limit: 128M

Author:
Problem type

You are standing in an open field west of a white house, with a boarded front door. There is a small mailbox here. Suddenly, your sword has begun to glow very brightly. You notice that a nasty-looking troll, brandishing a bloody axe, blocks your path to the house!

The troll grunts that he will not allow you to pass unless you solve his riddle. He shows you a mathematical expression given in infix notation and asks you to evaluate it. The expression contains integers in the range [-100,100] and the operators +-*/() (Note that / represents division truncated towards zero). Other than when representing a negative integer, +-*/ will only be used as binary operators.

But as you gleefully prepare to plug his expression into your scripting language's eval(), the troll announces that the order of operations have been changed! He gives you 4 integers a, b, c, d which represent the priority of the operators +, -, *, / respectively. Operators with higher priority are evaluated first, and those with equal priority are evaluated from left to right. Parentheses work normally.

Can you outsmart the troll and solve the riddle?

Input Specification

The first line of input will contain 4 integers a, b, c, d where 1 \le a, b, c, d \le 8.
The next line will contain an infix expression as described above. The expression will not exceed 110 characters.
The result of the expression as well as any intermediate values will be representable by a signed 32-bit integer.

Output Specification

Output one line containing the integer result of the mathematical expression.
Should the expression include division by zero, output the string attack troll with sword instead.

Sample Input 1

4 3 2 1
5+8*2

Sample Output 1

26

Explanation of Output for Sample Input 1

Since + has a priority of 4 and * has a lower priority of 2, the addition should be evaluated first.

Sample Input 2

4 3 2 8
69/(-2--2)

Sample Output 2

attack troll with sword

Explanation of Output for Sample Input 2

Despite / having the highest priority, the parentheses indicate that -2 - -2 should be evaluated first.
Since the expression then results in division by zero, the program should print attack troll with sword.


Comments

There are no comments at the moment.