Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.35 KB | None | 0 0
  1. package main.java;
  2.  
  3. import java.util.Arrays;
  4. import java.util.List;
  5.  
  6. public class Day05_2 {
  7.  
  8. private static List<Integer> input = Arrays
  9. .asList(3,225,1,225,6,6,1100,1,238,225,104,0,1101,40,71,224,1001,224,-111,224,4,224,1002,223,8,223,101,7,224,224,1,224,223,223,1102,66,6,225,1102,22,54,225,1,65,35,224,1001,224,-86,224,4,224,102,8,223,223,101,6,224,224,1,224,223,223,1102,20,80,225,101,92,148,224,101,-162,224,224,4,224,1002,223,8,223,101,5,224,224,1,224,223,223,1102,63,60,225,1101,32,48,225,2,173,95,224,1001,224,-448,224,4,224,102,8,223,223,1001,224,4,224,1,224,223,223,1001,91,16,224,101,-79,224,224,4,224,1002,223,8,223,101,3,224,224,1,224,223,223,1101,13,29,225,1101,71,70,225,1002,39,56,224,1001,224,-1232,224,4,224,102,8,223,223,101,4,224,224,1,223,224,223,1101,14,59,225,102,38,143,224,1001,224,-494,224,4,224,102,8,223,223,101,3,224,224,1,224,223,223,1102,30,28,224,1001,224,-840,224,4,224,1002,223,8,223,101,4,224,224,1,223,224,223,4,223,99,0,0,0,677,0,0,0,0,0,0,0,0,0,0,0,1105,0,99999,1105,227,247,1105,1,99999,1005,227,99999,1005,0,256,1105,1,99999,1106,227,99999,1106,0,265,1105,1,99999,1006,0,99999,1006,227,274,1105,1,99999,1105,1,280,1105,1,99999,1,225,225,225,1101,294,0,0,105,1,0,1105,1,99999,1106,0,300,1105,1,99999,1,225,225,225,1101,314,0,0,106,0,0,1105,1,99999,107,677,226,224,1002,223,2,223,1005,224,329,1001,223,1,223,8,226,226,224,102,2,223,223,1006,224,344,101,1,223,223,7,226,677,224,1002,223,2,223,1005,224,359,101,1,223,223,1007,677,226,224,1002,223,2,223,1005,224,374,1001,223,1,223,1007,677,677,224,1002,223,2,223,1006,224,389,101,1,223,223,1008,226,226,224,1002,223,2,223,1005,224,404,1001,223,1,223,108,677,226,224,1002,223,2,223,1006,224,419,1001,223,1,223,1108,677,226,224,102,2,223,223,1006,224,434,1001,223,1,223,108,226,226,224,1002,223,2,223,1005,224,449,101,1,223,223,7,677,677,224,1002,223,2,223,1006,224,464,1001,223,1,223,8,226,677,224,1002,223,2,223,1005,224,479,1001,223,1,223,107,226,226,224,102,2,223,223,1006,224,494,101,1,223,223,1007,226,226,224,1002,223,2,223,1005,224,509,1001,223,1,223,1107,226,677,224,102,2,223,223,1005,224,524,1001,223,1,223,108,677,677,224,1002,223,2,223,1005,224,539,101,1,223,223,1107,677,226,224,102,2,223,223,1005,224,554,1001,223,1,223,107,677,677,224,1002,223,2,223,1005,224,569,101,1,223,223,8,677,226,224,102,2,223,223,1005,224,584,1001,223,1,223,7,677,226,224,102,2,223,223,1006,224,599,101,1,223,223,1008,677,677,224,1002,223,2,223,1005,224,614,101,1,223,223,1008,677,226,224,102,2,223,223,1006,224,629,1001,223,1,223,1108,677,677,224,102,2,223,223,1006,224,644,101,1,223,223,1108,226,677,224,1002,223,2,223,1005,224,659,1001,223,1,223,1107,226,226,224,102,2,223,223,1006,224,674,1001,223,1,223,4,223,99,226);
  10.  
  11. private static final Integer DEFAULT_INPUT_INTEGER = 5;
  12.  
  13. public static void main(String[] args) throws Exception {
  14. for (Integer i = 0; i < input.size(); i++) {
  15. Integer firstParam;
  16. Integer secondParam;
  17. Integer thirdParam;
  18. Integer nextParamValue;
  19. Integer modeOfFirstParam = 0;
  20. Integer modeOfSecondParam = 0;
  21. Integer modeOfThirdParam = 0;
  22. boolean isInstruction;
  23.  
  24. if (input.get(i) == 99) {
  25. break;
  26.  
  27. } else {
  28. Integer oppcode = input.get(i);
  29.  
  30. if (oppcode.toString().length() > 2) {
  31. String instruction = String.format("%05d", oppcode).replaceAll("-","0");
  32. modeOfThirdParam = Integer.parseInt(instruction.substring(0,1));
  33. modeOfSecondParam = Integer.parseInt(instruction.substring(1,2));
  34. modeOfFirstParam = Integer.parseInt(instruction.substring(2, 3));
  35. oppcode = Integer.parseInt(instruction.substring(3,5));
  36. isInstruction = true;
  37.  
  38. } else {
  39. isInstruction = false;
  40. }
  41.  
  42. firstParam = getValueAtIndex(input.get(i+1), modeOfFirstParam == 1);
  43. nextParamValue = input.get(i+1);
  44.  
  45.  
  46. if (oppcode == 1 || oppcode == 2 || oppcode == 7 || oppcode == 8) {
  47. secondParam = getValueAtIndex(input.get(i+2), modeOfSecondParam == 1);
  48. thirdParam = modeOfThirdParam == 1 ? i+3 : input.get(i+3);
  49.  
  50. if (oppcode == 1) {
  51. input.set(thirdParam, firstParam + secondParam);
  52. } else if (oppcode == 2) {
  53. input.set(thirdParam, firstParam * secondParam);
  54. } else if (oppcode == 7) {
  55. if (firstParam < secondParam) {
  56. input.set(thirdParam, 1);
  57. } else {
  58. input.set(thirdParam, 0);
  59. }
  60. } else if (oppcode == 8) {
  61. if (firstParam == secondParam) {
  62. input.set(thirdParam, 1);
  63. } else {
  64. input.set(thirdParam, 0);
  65. }
  66. }
  67.  
  68. } else if (oppcode == 3) {
  69. input.set(nextParamValue, DEFAULT_INPUT_INTEGER);
  70.  
  71. } else if (oppcode == 4) {
  72. Integer inputValue = getValueAtIndex(nextParamValue, isInstruction);
  73. System.out.println(inputValue);
  74.  
  75. } else if (oppcode == 5) {
  76. secondParam = getValueAtIndex(input.get(i+2), modeOfSecondParam == 1);
  77. if (firstParam != 0) {
  78. i = secondParam -1;
  79. } else {
  80. i+=2;
  81. }
  82. continue;
  83.  
  84. } else if (oppcode == 6) {
  85. secondParam = getValueAtIndex(input.get(i+2), modeOfSecondParam == 1);
  86.  
  87. if (firstParam == 0) {
  88. i = secondParam -1;
  89. } else {
  90. i+=2;
  91. }
  92. continue;
  93.  
  94. } else {
  95. throw new Exception("Wrong oppcode (" + oppcode + ") at " + i + "line.");
  96.  
  97. }
  98. i = increaseIndex(i, oppcode);
  99. }
  100. }
  101. }
  102.  
  103. private static Integer increaseIndex(Integer index, Integer oppcode) {
  104.  
  105. if (oppcode == 1 || oppcode == 2 || oppcode == 7 || oppcode == 8) {
  106. index += 3;
  107. } else {
  108. index += 1;
  109. }
  110. return index;
  111. }
  112.  
  113. private static Integer getValueAtIndex(Integer index, boolean isImmediateMode) {
  114. return isImmediateMode ? index : input.get(index);
  115. }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement