Skip to content

Instantly share code, notes, and snippets.

@tth05
Last active July 23, 2018 23:38
Show Gist options
  • Save tth05/4f20440329ccff930b5f21668dd27795 to your computer and use it in GitHub Desktop.
Save tth05/4f20440329ccff930b5f21668dd27795 to your computer and use it in GitHub Desktop.
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class Main {
/*
Example Input: Doubling numbers infinitely (doubles the inputted number infinitely: 3 -> 6, 12, 24, 48, ...) (Ctrl + V):
I0
A0 A0
A0 C3 G1 G1 G1 A0
A0 V0 S0 V0 O0 A0
A0 A1 G-3 G-3 G-3 A0
G-3
------------
3
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Insert A0A0 code followed by a series of \"------------\":");
List<String> lines = new ArrayList<>();
int index = 0;
while (scanner.hasNext()) {
String input = scanner.nextLine();
if (input.startsWith("-")) break;
if (input.startsWith(">")) {
index = lines.size();
input = input.substring(1);
}
lines.add(input);
}
execute(lines, index, scanner);
}
private static void execute(List<String> lines, int index, Scanner scanner) {
lines = lines.stream().map(s -> s.replace(" ", "")).collect(Collectors.toList());
BigInteger operand = null;
Pattern p = Pattern.compile("-?\\d+");
while (true) {
String oldLine = lines.get(index);
if (oldLine.equals("")) break;
Matcher m = p.matcher(oldLine);
m.find();
char c = oldLine.charAt(0);
BigInteger arg = new BigInteger(m.group());
String line = oldLine.substring(m.end());
lines.set(index, line);
switch (c) {
case 'A':
if (index + arg.intValue() > lines.size()) System.exit(0);
else if (index + arg.intValue() == lines.size()) lines.add(line);
else lines.set(index + arg.intValue(), lines.get(index + arg.intValue()) + line);
break;
case 'C':
if (index + arg.intValue() >= 0 && index + arg.intValue() < lines.size())
lines.remove(index + arg.intValue());
break;
case 'G':
index += arg.intValue();
break;
case 'V':
if (operand == null) operand = arg;
lines.set(index, line.replaceFirst(p.pattern(), operand.toString()));
break;
case 'S':
if (operand != null)
operand = operand.add(arg);
break;
case 'D':
if (operand != null)
operand = operand.subtract(arg);
break;
case 'M':
if (operand != null)
operand = operand.multiply(arg);
break;
case 'L':
if (operand != null)
operand = new BigInteger(String.valueOf(operand.compareTo(arg)));
break;
case 'I':
if (scanner.hasNext()) {
if (arg.intValue() == 0) {
operand = new BigInteger(String.valueOf(scanner.nextInt()));
}
if (arg.intValue() == 1)
operand = new BigInteger(String.valueOf((int) scanner.next().charAt(0)));
}
break;
case 'O':
System.out.println(arg);
break;
case 'P':
System.out.print((char) (arg.intValue() % 256));
break;
}
if (c != 'G') index++;
if (index >= lines.size()) break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment