Sane's Monthly Algorithms Challenge: November 2008
Befungulator (Beginner Level)
For no apparent reason (why else?), you have decided to create your own
interpreter which evaluates mathematical expressions by “befungulating”
an inputted grid of characters.
The grid is given to you as an n x m (n rows, m columns) matrix of the following characters:
|0-9||Each of the 10 digits represents a single digit number as usual|
|>||Continue reading instructions in the right direction|
|<||Continue reading instructions in the left direction|
|^||Continue reading instructions in the upward direction|
|v||Continue reading instructions in the downward direction|
|X||Print the integer value followed by a newline. Stop and exit.|
Any other characters should be ignored.
To befungulate a grid of characters, you start at the top-left of the grid, and read right until you find a valid character. If the character is an operator or operand, you intuitively use the character in the evaluation of the mathematical expression (i.e. as you would with a normal calculator).
If you hit a directional operator (
v), you change the direction of reading the grid.
You may assume:
- All operands are single digits.
- The sequence of operators and operands makes sense.
(e.g. 5+3*9 is valid. 53, +3*, and 3**9 are not)
With exception to an expression interrupted by an error (see Error Handling, below).
- The order of operations does not matter (evaluate left to right).
- An X will not be interpreted if no value has been defined.
- The calculation will fit within a normal integer data type.
If you end up reading off the grid, then output
Invalid Directionwith a newline and quit.
If you divide by zero, then output
Zero Division Errorwith a newline and quit.
If you modulate by zero, then output
Invalid Modulowith a newline and quit.
If you end up in an infinite loop, then output
Infinite Loopwith a newline and quit.
Errors occur in the order that they are interpreted (e.g. If there is both Zero Division and Invalid Direction, Zero Division must be interpreted first).
InputOn the first line are integers
On the next
mcharacters each, representing the row of characters. These characters are listed from “left” to “right”. The rows are listed from “top” to “bottom”.
OutputSee problem statement.
Sample Input 1
Sample Output 1
Explanation of Sample Data 1:
Sample Input 2
Sample Output 2
Sample Input 3
Sample Output 3
Explanation of Sample Data 3
Sample Input 4
Sample Output 4
Point Value: 10
Time Limit: 2.00s
Memory Limit: 16M
Added: Jan 16, 2009
C++03, PAS, C, HASK, ASM, RUBY, PYTH2, JAVA, PHP, SCM, CAML, PERL, C#, C++11, PYTH3