Created
September 6, 2018 16:34
-
-
Save Hc747/4fa79b8e51d8a0119d0daa954b4fcb18 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Stack; | |
public class Mindmelt { | |
public static void main(String[] args) { | |
//Scanner scanner = new Scanner(System.in); | |
Interpreter i = new Interpreter(new char[0]/*scanner.nextLine().toCharArray()*/); | |
int index = 0; | |
char[] commands = ">+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.".toCharArray(); | |
do { | |
index = i.interpret(index, commands); | |
} while (index > -1); | |
} | |
static class Interpreter { | |
Interpreter(char[] i) { | |
this.i = i; | |
} | |
char[] i; | |
int[] m = new int[30_000]; | |
int p, r; | |
Stack<Integer> s = new Stack<>(); | |
int interpret(int index, char[] command) { | |
char c = command[index]; | |
switch (c) { | |
case '+': m[p]++; break; | |
case '-': m[p]--; break; | |
case '>': p++; break; | |
case '<': p--; break; | |
case '[': | |
if (m[p] != 0) | |
s.push(index); | |
break; | |
case ']': | |
int pos = s.pop(); | |
if (m[p] != 0) | |
return pos; | |
break; | |
case '.': System.out.print(Character.toChars(m[p])); break; | |
case ',': m[p] = i[r++]; break; | |
default: break; | |
} | |
return index + 1 < command.length ? index + 1 : -1; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment