Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Advent5_2 {
- public static boolean endProgram = false;
- public static int input = 0;
- public static void main(String[] args) {
- if (args.length != 2) {
- System.out.println("Args must be a file and an int!");
- return;
- }
- input = Integer.parseInt(args[1]);
- Scanner scan = null;
- try {
- scan = new Scanner(new File(args[0]));
- String raw = scan.next().trim();
- String[] rawArr = raw.split(",");
- int[] ints = new int[rawArr.length];
- for (int i = 0; i < ints.length; i++) {
- ints[i] = Integer.parseInt(rawArr[i]);
- }
- for (int pointer = 0; pointer < ints.length;) {
- if(!endProgram)
- pointer = processInstruction(ints, pointer);
- else
- break;
- }
- } catch (FileNotFoundException e) {
- System.out.println("Not a File!");
- } finally {
- if (scan != null)
- scan.close();
- }
- }
- private static int processInstruction(int[] ints, int pointer) {
- int[] instruction = getInstructionArray(ints[pointer]);
- switch(instruction[3] *10 +instruction[4]) {
- case 1:
- add(ints, pointer, instruction);
- return pointer + 4;
- case 2:
- mult(ints, pointer, instruction);
- return pointer + 4;
- case 3:
- ints[ints[pointer+1]] = getInput();
- return pointer + 2;
- case 4:
- output(ints, pointer, instruction);
- return pointer + 2;
- case 5:
- return jumpIfTrue(ints, pointer, instruction);
- case 6:
- return jumpIfFalse(ints, pointer, instruction);
- case 7:
- lessThan(ints, pointer, instruction);
- return pointer + 4;
- case 8:
- greaterThan(ints, pointer, instruction);
- return pointer + 4;
- case 99:
- endProgram = true;
- break;
- default:
- System.out.println("Bad instruction: " + instruction[4]);
- }
- return 1;
- }
- private static void greaterThan(int[] ints, int pointer, int[] instruction) {
- // TODO Auto-generated method stub
- }
- private static void lessThan(int[] ints, int pointer, int[] instruction) {
- // TODO Auto-generated method stub
- }
- private static int jumpIfFalse(int[] ints, int pointer, int[] instruction) {
- // TODO Auto-generated method stub
- return 0;
- }
- private static int jumpIfTrue(int[] ints, int pointer, int[] instruction) {
- // TODO Auto-generated method stub
- return 0;
- }
- private static void mult(int[] ints, int pointer, int[] instruction) {
- boolean im0 = instruction[2] > 0;
- boolean im1 = instruction[1] > 0;
- boolean im2 = instruction[0] > 0;
- ints[im2 ? pointer + 3 : ints[pointer + 3]] = ints[im1 ? pointer + 2 : ints[pointer+2]] * ints[im0 ? pointer + 1 : ints[pointer+1]];
- }
- private static void add(int[] ints, int pointer, int[] instruction) {
- boolean im0 = instruction[2] > 0;
- boolean im1 = instruction[1] > 0;
- boolean im2 = instruction[0] > 0;
- ints[im2 ? pointer + 3 : ints[pointer + 3]] = ints[im1 ? pointer + 2 : ints[pointer+2]] + ints[im0 ? pointer + 1 : ints[pointer+1]];
- }
- private static int getInput() {
- return input;
- }
- private static void output(int[] ints, int pointer, int[] instructions) {
- boolean im0 = instructions[2] > 0;
- System.out.println("Output: " + (im0 ? ints[pointer+1] : ints[ints[pointer+1]]));
- }
- private static int[] getInstructionArray(int inst) {
- char[] c = Integer.toString(inst).toCharArray();
- int[] instruction = new int[] { 0, 0, 0, 0, 0 };
- for (int i = 0; i < c.length; i++) {
- instruction[instruction.length - i - 1] = c[c.length - i - 1] - '0';
- }
- return instruction;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement