Advertisement
NilsKirchhoff

AoC 2019 - Day 2 - ProcessingSketch

Dec 2nd, 2019
530
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.46 KB | None | 0 0
  1. String fileName = "input.txt";
  2. //String fileName = "testdata.txt";
  3. BufferedReader reader;
  4. String line;
  5. IntList values;
  6.  
  7. void setup() {
  8.   reader = createReader(fileName);
  9.   try {
  10.         line = reader.readLine();
  11.       }
  12.       catch (IOException e) {
  13.         line = null;
  14.       }
  15.      
  16.   values = new IntList();
  17.   String[] listSplit = split(line, ',');
  18.   for( int value : int(listSplit) ) {
  19.     values.append(value);
  20.   }
  21.    
  22.   /* day 2 _ 1 -   answer 4138658
  23.   values.set(1, 12);
  24.   values.set(2, 2);
  25.   int i = 0;
  26.   int Opcode = values.get(i);
  27.   while(Opcode != 99) {
  28.     switch(Opcode) {
  29.       case 1:
  30.       values.set(values.get(i+3), operation(values.get(i+1), values.get(i+2), Opcode));
  31.       i = i+4;
  32.       Opcode = values.get(i);
  33.       break;
  34.       case 2:
  35.       values.set(values.get(i+3), operation(values.get(i+1), values.get(i+2), Opcode));
  36.       i = i+4;
  37.       Opcode  = values.get(i);
  38.       break;
  39.       }
  40.   }
  41.   println(values.get(0));
  42.   */
  43.  
  44.   // day 2 _ 2 input answer 7264
  45.  
  46.   boolean stop = false;
  47.   int noun = 0;
  48.   int verb = 0;
  49.   int output = 0;
  50.   int expectedOutput = 19690720;
  51.   int result = 0;
  52.   int Opcode = 0;
  53.     while(output  != expectedOutput && !stop ) {
  54.       IntList copyOfValues = new IntList();
  55.       for( int value : values) {
  56.          copyOfValues.append(value);
  57.       }
  58.       int i = 0;
  59.       Opcode = copyOfValues.get(i);
  60.       copyOfValues.set(1, noun);
  61.       copyOfValues.set(2, verb);
  62.       while(Opcode != 99) {
  63.         switch(Opcode) {
  64.           case 1:
  65.           copyOfValues.set(copyOfValues.get(i+3), copyOfValues.get(copyOfValues.get(i+1)) + copyOfValues.get(copyOfValues.get(i+2)));
  66.           i = i+4;
  67.           Opcode = copyOfValues.get(i);
  68.           break;
  69.           case 2:
  70.           copyOfValues.set(copyOfValues.get(i+3), copyOfValues.get(copyOfValues.get(i+1)) * copyOfValues.get(copyOfValues.get(i+2)));
  71.           i = i+4;
  72.           Opcode  = copyOfValues.get(i);
  73.           break;
  74.           }
  75.       }
  76.       result = 100*noun+verb;
  77.       if(noun < 99)  {
  78.         noun++;
  79.       } else {
  80.          verb++;
  81.          noun = 0;
  82.       }
  83.       if (verb > 99)  {
  84.         stop = true;
  85.       }
  86.       output = copyOfValues.get(0);
  87.     }
  88.     println(result);
  89. }
  90.  
  91. int operation(int pos1, int pos2, int Opcode){
  92.   switch(Opcode) {
  93.       case 1:
  94.         return values.get(pos1) + values.get(pos2);
  95.       case 2:
  96.         return values.get(pos1) * values.get(pos2);
  97.   }
  98.   return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement