Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.50 KB | None | 0 0
  1. package com.aoc;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.util.List;
  6. import java.util.Scanner;
  7. import java.util.ArrayList;
  8.  
  9. class IntcodeCalculator {
  10.     public int calculate(List <Integer> input, int param1, int param2) {
  11.         List <Integer> intcode = new ArrayList <Integer> (input);
  12.  
  13.         intcode.set(1, param1);
  14.         intcode.set(2, param2);
  15.  
  16.         for (int i = 0; i<intcode.size(); i += 4) {
  17.             int opcode = intcode.get(i);
  18.             if (opcode > 2 || opcode == 0) { break; }
  19.  
  20.             int x = intcode.get(intcode.get(i+1));
  21.             int y = intcode.get(intcode.get(i+2));
  22.             int pos = intcode.get(i+3);
  23.  
  24.             if (opcode == 1)      { intcode.set(pos, x + y); }
  25.             else if (opcode == 2) { intcode.set(pos, x * y); }
  26.         }   return intcode.get(0);
  27.     }
  28. }
  29.  
  30. class AOC1 {
  31.     private static int param1 = 12;
  32.     private static int param2 = 2;
  33.     int solve(List <Integer> input) {
  34.         IntcodeCalculator calculator = new IntcodeCalculator();
  35.         return calculator.calculate(input, param1, param2);
  36.     }
  37. }
  38. class AOC2 {
  39.     private static int goal = 19690720;
  40.     int solve(List <Integer> input) {
  41.         IntcodeCalculator calculator = new IntcodeCalculator();
  42.         int ans = -1;
  43.         for (int param1=0; param1<100; ++param1) {
  44.             for (int param2 = 0; param2 < 100; ++param2) {
  45.                 if (calculator.calculate(input, param1, param2) == goal) {
  46.                     ans = param1 * 100 + param2;
  47.                     break;
  48.                 }
  49.             }
  50.         }   return ans;
  51.     }
  52. }
  53.  
  54. class AOC2_2 {  // thanks reddit for the idea <3 u/aoc_anon
  55.     private static int goal = 19690720;
  56.     int solve(List <Integer> input) {
  57.         IntcodeCalculator calculator = new IntcodeCalculator();
  58.         int ans = -1;
  59.  
  60.         //  if param1 is a, param2 is b, the answer should be something like:
  61.         //  x*a + y*b + z
  62.         //  3 unknowns => 3 eq needed
  63.         //  a = 0, b = 0: z
  64.         //  a = 1, b = 0: x+z
  65.         //  a = 0, b = 1: y+z
  66.  
  67.         int z = calculator.calculate(input, 0, 0);
  68.         int x = calculator.calculate(input, 1, 0) - z;
  69.         int y = calculator.calculate(input, 0, 1) - z;
  70.  
  71.         //  we need x*a + y*b + z == answer
  72.  
  73.         for (int a = 0; a <= 99; ++a) {
  74.             int rem = goal - x*a - z;
  75.             if (rem%y == 0) {
  76.                 int b = rem/y;
  77.                 if (b >= 0 && b <= 99) {
  78.                     ans = a*100 + b;
  79.                     break;
  80.                 }
  81.             }
  82.         }
  83.  
  84.         return ans;
  85.     }
  86. }
  87.  
  88. public class Main {
  89.     private static List <Integer> parseInput() {
  90.         List <Integer> answer = new ArrayList <Integer> ();
  91.  
  92.         try {
  93.             Scanner scanner = new Scanner(new File("input.txt"));
  94.             if (scanner.hasNextLine()) {
  95.                 String input = scanner.nextLine();
  96.                 for (String num : input.split(",")) {
  97.                     answer.add(Integer.parseInt(num));
  98.                 }
  99.             }
  100.             else System.out.println("File data line missing!");
  101.         }   catch (FileNotFoundException e) {
  102.             System.out.println("File missing!");
  103.         }
  104.  
  105.         return answer;
  106.     }
  107.  
  108.     public static void main(String[] args) {
  109.         AOC1 solver  = new AOC1();
  110.         AOC2_2 solver2 = new AOC2_2();
  111.         System.out.println(solver.solve(parseInput()));
  112.         System.out.println(solver2.solve(parseInput()));
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement