You are given a rectangular grid with entries that are whole integers. You start from the cell at row r and column c. You have four operators: +, -, *, /. These operators have double meanings, their usual arithmetic ones and:
+: move to the cell on the right (c increases by one or wraps to the other side)
-: move to the cell on the left (c decreases by one or wraps to the other side)
*: move the cell upwards (r decreases by one or wraps to the other side)
/: move the cell downwards (r increases by one or wraps to the other side)
Exercise 1:
Given a string of operators, and applying them to the cells of the current position of the player (and respecting the rules of precedence) return the result of the expression created by the string.
Example:
Here's the grid, top left is 0, 0
1 2
3 4
You start at (1,0).
the cell that has the value 3.
The input expression is: +*.
The resulting expression is: 3 + 4 * 2.
The result of the expression is: 11. (not 14).
Your program must be run in the command line to parse the following input (through standard input):
- Two numbers representing the dimensions of the grid
- Two numbers representing the starting position (guaranteed to be less than the dimensions of the grid)
- The number of operators in the expression
- The characters representing the operators
- The grid numbers in sequence (horizontally through rows then moves to the next row)
The standard stream for this example would be:
2 2 1 0 2 +* 1 2 3 4
Parsing this input, the program must return just 11.
Note that I removed the "-" operator from the expression to keep in sync with the text above.
The next Exercise will be based on the solution of this first one.
Some input into the program has slipped my mind while writing the exercise. Specifically the input expression. Thanks @xterm for pointing it out. Everything is sorted out now. Skim through the example to see how the input file was updated. I should have just posted again for people to see the difference, will keep that in mind next time I correct something.