Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.66 KB | None | 0 0
  1. // Day 2 Advent of Code 2019, Author: Jose Oliver
  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileReader;
  6. import java.io.IOException;
  7.  
  8. public class Solution {
  9.     public static void main(String[] args) throws IOException, FileNotFoundException, NumberFormatException {
  10.         File file = new File("input.txt");
  11.         FileReader reader = new FileReader(file);
  12.         BufferedReader bufferedReader = new BufferedReader(reader);
  13.         String input = bufferedReader.readLine();
  14.         String[] filteredinput = input.split(",",0);
  15.         // Replacement of position 1 and 2 as per request of the challenge
  16.         int[] opcode = new int[filteredinput.length];
  17.         int position1;
  18.         int position2;
  19.         int position3;
  20.         int sum;
  21.         int multiplied;
  22.         int noun;
  23.         int verb;
  24.         for(int i = 0; i < filteredinput.length; i++) {
  25.             opcode[i] = Integer.parseInt(filteredinput[i]);          
  26.         }
  27.         for(int i = 0; i < 100; i++){
  28.             noun = i;
  29.             for(int j = 0; i < 100; i++){
  30.                 verb = j;
  31.                 for(int k = 0; i < opcode.length;) {
  32.                     opcode[1] = noun;
  33.                     opcode[2] = verb;
  34.                     if(opcode[k] == 1) {
  35.                         position1 = opcode[k + 1];
  36.                         position2 = opcode[k + 2];
  37.                         position3 = opcode[k + 3];
  38.                         sum = opcode[position1] + opcode[position2];
  39.                         opcode[position3] = sum;
  40.                         k += 4;              
  41.                     } else if(opcode[k] == 2) {
  42.                         position1 = opcode[k + 1];
  43.                         position2 = opcode[k + 2];
  44.                         position3 = opcode[k + 3];
  45.                         multiplied = opcode[position1] * opcode[position2];
  46.                         opcode[position3] = multiplied;
  47.                         k += 4;                
  48.                     } else if(opcode[k] == 99) {
  49.                         break;
  50.                     }
  51.                  
  52.                 }
  53.                 if(opcode[0] == 19690720){
  54.                     System.out.println(100 * noun + verb);
  55.                     break;
  56.                 } else {
  57.                     opcode = reset(opcode, filteredinput);
  58.                 }
  59.             }
  60.         }
  61.         bufferedReader.close();
  62.     }
  63.  
  64.     static int[] reset(int[] intarray, String[] stringarray) {
  65.         for(int i = 0; i < stringarray.length; i++) {
  66.             intarray[i] = Integer.parseInt(stringarray[i]);
  67.         }
  68.         return intarray;
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement