Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. public class Advent5_2 {
  2.  
  3. public static boolean endProgram = false;
  4. public static int input = 0;
  5.  
  6. public static void main(String[] args) {
  7. if (args.length != 2) {
  8. System.out.println("Args must be a file and an int!");
  9. return;
  10. }
  11. input = Integer.parseInt(args[1]);
  12. Scanner scan = null;
  13. try {
  14. scan = new Scanner(new File(args[0]));
  15. String raw = scan.next().trim();
  16. String[] rawArr = raw.split(",");
  17. int[] ints = new int[rawArr.length];
  18. for (int i = 0; i < ints.length; i++) {
  19. ints[i] = Integer.parseInt(rawArr[i]);
  20. }
  21. for (int pointer = 0; pointer < ints.length;) {
  22. if(!endProgram)
  23. pointer = processInstruction(ints, pointer);
  24. else
  25. break;
  26. }
  27. } catch (FileNotFoundException e) {
  28. System.out.println("Not a File!");
  29. } finally {
  30. if (scan != null)
  31. scan.close();
  32. }
  33. }
  34.  
  35. private static int processInstruction(int[] ints, int pointer) {
  36. int[] instruction = getInstructionArray(ints[pointer]);
  37. switch(instruction[3] *10 +instruction[4]) {
  38. case 1:
  39. add(ints, pointer, instruction);
  40. return pointer + 4;
  41. case 2:
  42. mult(ints, pointer, instruction);
  43. return pointer + 4;
  44. case 3:
  45. ints[ints[pointer+1]] = getInput();
  46. return pointer + 2;
  47. case 4:
  48. output(ints, pointer, instruction);
  49. return pointer + 2;
  50. case 5:
  51. return jumpIfTrue(ints, pointer, instruction);
  52. case 6:
  53. return jumpIfFalse(ints, pointer, instruction);
  54. case 7:
  55. lessThan(ints, pointer, instruction);
  56. return pointer + 4;
  57. case 8:
  58. greaterThan(ints, pointer, instruction);
  59. return pointer + 4;
  60. case 99:
  61. endProgram = true;
  62. break;
  63. default:
  64. System.out.println("Bad instruction: " + instruction[4]);
  65. }
  66. return 1;
  67. }
  68.  
  69. private static void greaterThan(int[] ints, int pointer, int[] instruction) {
  70. // TODO Auto-generated method stub
  71.  
  72. }
  73.  
  74. private static void lessThan(int[] ints, int pointer, int[] instruction) {
  75. // TODO Auto-generated method stub
  76.  
  77. }
  78.  
  79. private static int jumpIfFalse(int[] ints, int pointer, int[] instruction) {
  80. // TODO Auto-generated method stub
  81. return 0;
  82. }
  83.  
  84. private static int jumpIfTrue(int[] ints, int pointer, int[] instruction) {
  85. // TODO Auto-generated method stub
  86. return 0;
  87. }
  88.  
  89. private static void mult(int[] ints, int pointer, int[] instruction) {
  90. boolean im0 = instruction[2] > 0;
  91. boolean im1 = instruction[1] > 0;
  92. boolean im2 = instruction[0] > 0;
  93. ints[im2 ? pointer + 3 : ints[pointer + 3]] = ints[im1 ? pointer + 2 : ints[pointer+2]] * ints[im0 ? pointer + 1 : ints[pointer+1]];
  94. }
  95.  
  96. private static void add(int[] ints, int pointer, int[] instruction) {
  97. boolean im0 = instruction[2] > 0;
  98. boolean im1 = instruction[1] > 0;
  99. boolean im2 = instruction[0] > 0;
  100. ints[im2 ? pointer + 3 : ints[pointer + 3]] = ints[im1 ? pointer + 2 : ints[pointer+2]] + ints[im0 ? pointer + 1 : ints[pointer+1]];
  101. }
  102.  
  103. private static int getInput() {
  104. return input;
  105. }
  106.  
  107. private static void output(int[] ints, int pointer, int[] instructions) {
  108. boolean im0 = instructions[2] > 0;
  109. System.out.println("Output: " + (im0 ? ints[pointer+1] : ints[ints[pointer+1]]));
  110. }
  111. private static int[] getInstructionArray(int inst) {
  112. char[] c = Integer.toString(inst).toCharArray();
  113. int[] instruction = new int[] { 0, 0, 0, 0, 0 };
  114. for (int i = 0; i < c.length; i++) {
  115. instruction[instruction.length - i - 1] = c[c.length - i - 1] - '0';
  116. }
  117. return instruction;
  118. }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement