Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package foo;
- import java.util.Scanner;
- /**
- * Procrastination: I'll add a javadoc description later.<br>
- * hello @ foo
- *
- * @author Unihedron<vincentyification@gmail.com>
- */
- public class hello {
- private static final java.util.regex.Pattern WORD = java.util.regex.Pattern.compile("([A-Za-z]\\d)");
- private static Object magic(final Object[][] grid, final String toParse) {
- if ( !toParse.matches("=(\\w+)([-+*/]\\w+)*")) return toParse;
- String eval = toParse;
- while (true) {
- final java.util.regex.Matcher matcher = hello.WORD.matcher(eval);
- if (matcher.find()) eval =
- eval.replaceFirst(hello.WORD.pattern(),
- grid[Integer.parseInt(Character.toString(matcher.group(1).charAt(1))) - 1][matcher.group(1).charAt(0) - 'A'].toString());
- else break;
- }
- final javax.script.ScriptEngine mgr = new javax.script.ScriptEngineManager().getEngineByName("JavaScript");
- try {
- final String evald = mgr.eval(eval.toString().substring(1)).toString();
- return evald.substring(0, evald.length() - 2);
- } catch (final javax.script.ScriptException e) {
- e.printStackTrace();
- }
- return null;
- }
- public static void main(final String args[]) {
- final Scanner input = new Scanner(System.in);
- final Object[][] grid = new Object[3][3];
- for (int i = 0; i < 3; i++ )
- for (int j = 0; j < 3; j++ ) {
- final String toParse = input.next();
- grid[i][j] = toParse.matches("-?\\d+") ? Integer.valueOf(toParse) : hello.magic(grid, toParse);
- }
- for (int i = 0; i < 3; i++ ) {
- for (int j = 0; j < 3; j++ ) {
- System.out.print(grid[i][j]);
- System.out.print(' ');
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement