Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.TreeMap;
- public class quack implements Runnable {
- BufferedReader in;
- PrintWriter out;
- int[] queueArr = new int[10];
- int[] queueNew;
- int queueSize = 10;
- int queueLSize = 10;
- int queueRSize = 0;
- int queueH = 9;
- int queueT = 9;
- int mod = 65536;
- int[] regs = new int[26];
- String[] commands = new String[100000];
- int pos = 0;
- int n = 0;
- TreeMap<String, Integer> labels = new TreeMap<String, Integer>();
- public static void main(String[] args) {
- new Thread(new quack()).run();
- }
- public void run() {
- try {
- in = new BufferedReader(new FileReader("input.txt"));
- out = new PrintWriter(new FileWriter("output.txt"));
- solve();
- //System.out.println(Integer.MIN_VALUE);
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- out.flush();
- out.close();
- }
- }
- void put(int num) {
- if (queueLSize == 0) {
- queueSize = (queueH - queueT) * 2;
- queueNew = new int[queueSize];
- for (int i = queueH; i > queueT; i--) {
- queueNew[queueSize - (queueH - i) - 1] = queueArr[i];
- }
- queueLSize = queueSize / 2;
- queueRSize = 0;
- queueH = queueSize - 1;
- queueT = queueSize / 2 - 1;
- queueArr = queueNew;
- }
- queueArr[queueT--] = ((mod + num) % mod);
- queueLSize--;
- }
- int get() {
- if (queueRSize >= queueSize / 2) {
- queueSize = (queueH - queueT) * 2;
- queueNew = new int[queueSize];
- for (int i = queueH; i > queueT; i--) {
- queueNew[queueSize - (queueH - i) - 1] = queueArr[i];
- }
- queueLSize = queueSize / 2;
- queueRSize = 0;
- queueH = queueSize - 1;
- queueT = queueSize / 2 - 1;
- queueArr = queueNew;
- }
- queueRSize++;
- return ((mod + queueArr[queueH--]) % mod);
- }
- void add() {
- put(get() + get());
- }
- void substract() {
- put(get() - get());
- }
- void multiply() {
- put((int)(((long)get() * (long)get()) % (long)mod));
- }
- void divide() {
- int x = get(), y = get();
- put((y == 0) ? 0 : (x / y));
- }
- void modulo() {
- int x = get(), y = get();
- put((y == 0) ? 0 : (x % y));
- }
- void setReg(char id) {
- regs[id - 'a'] = get();
- }
- void getReg(char id) {
- put(regs[id - 'a']);
- }
- void P() {
- out.print(Integer.toString(get()) + (char)10);
- }
- void P(char id) {
- out.print(Integer.toString(regs[id - 'a']) + (char)10);
- }
- void C() {
- out.print((char) (get() % 256));
- }
- void C(char id) {
- out.print((char) (regs[id - 'a'] % 256));
- }
- void label(String id) {
- labels.put(id, n - 1);
- }
- void J(String id) {
- pos = labels.get(id);
- }
- void Z(char regId, String labelId) {
- if (regs[regId - 'a'] == 0)
- J(labelId);
- }
- void E(char regId1, char regId2, String labelId) {
- if (regs[regId1 - 'a'] == regs[regId2 - 'a'])
- J(labelId);
- }
- void G(char regId1, char regId2, String labelId) {
- if (regs[regId1 - 'a'] > regs[regId2 - 'a'])
- J(labelId);
- }
- void Q() {
- pos = n + 1;
- }
- void solve() throws NumberFormatException, IOException {
- String command = in.readLine();
- while (command != null) {
- commands[n++] = command;
- if (command.charAt(0) == ':')
- label(command.substring(1));
- command = in.readLine();
- }
- for (pos = 0; pos < n; pos++) {
- char c = commands[pos].charAt(0);
- switch (c) {
- case '+':
- add();
- break;
- case '-':
- substract();
- break;
- case '*':
- multiply();
- break;
- case '/':
- divide();
- break;
- case '%':
- modulo();
- break;
- case '>':
- setReg(commands[pos].charAt(1));
- break;
- case '<':
- getReg(commands[pos].charAt(1));
- break;
- case 'P': {
- if (commands[pos].length() == 1)
- P();
- else
- P(commands[pos].charAt(1));
- break;}
- case 'C': {
- if (commands[pos].length() == 1)
- C();
- else
- C(commands[pos].charAt(1));
- break;}
- case ':':
- break;
- case 'J':
- J(commands[pos].substring(1));
- break;
- case 'Z':
- Z(commands[pos].charAt(1), commands[pos].substring(2));
- break;
- case 'E':
- E(commands[pos].charAt(1), commands[pos].charAt(2),
- commands[pos].substring(3));
- break;
- case 'G':
- G(commands[pos].charAt(1), commands[pos].charAt(2),
- commands[pos].substring(3));
- break;
- case 'Q':
- Q();
- break;
- default:
- put(Integer.parseInt(commands[pos]));
- break;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment