Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.aoc;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.List;
- import java.util.Scanner;
- import java.util.ArrayList;
- class IntcodeCalculator {
- public int calculate(List <Integer> input, int param1, int param2) {
- List <Integer> intcode = new ArrayList <Integer> (input);
- intcode.set(1, param1);
- intcode.set(2, param2);
- for (int i = 0; i<intcode.size(); i += 4) {
- int opcode = intcode.get(i);
- if (opcode > 2 || opcode == 0) { break; }
- int x = intcode.get(intcode.get(i+1));
- int y = intcode.get(intcode.get(i+2));
- int pos = intcode.get(i+3);
- if (opcode == 1) { intcode.set(pos, x + y); }
- else if (opcode == 2) { intcode.set(pos, x * y); }
- } return intcode.get(0);
- }
- }
- class AOC1 {
- private static int param1 = 12;
- private static int param2 = 2;
- int solve(List <Integer> input) {
- IntcodeCalculator calculator = new IntcodeCalculator();
- return calculator.calculate(input, param1, param2);
- }
- }
- class AOC2 {
- private static int goal = 19690720;
- int solve(List <Integer> input) {
- IntcodeCalculator calculator = new IntcodeCalculator();
- int ans = -1;
- for (int param1=0; param1<100; ++param1) {
- for (int param2 = 0; param2 < 100; ++param2) {
- if (calculator.calculate(input, param1, param2) == goal) {
- ans = param1 * 100 + param2;
- break;
- }
- }
- } return ans;
- }
- }
- class AOC2_2 { // thanks reddit for the idea <3 u/aoc_anon
- private static int goal = 19690720;
- int solve(List <Integer> input) {
- IntcodeCalculator calculator = new IntcodeCalculator();
- int ans = -1;
- // if param1 is a, param2 is b, the answer should be something like:
- // x*a + y*b + z
- // 3 unknowns => 3 eq needed
- // a = 0, b = 0: z
- // a = 1, b = 0: x+z
- // a = 0, b = 1: y+z
- int z = calculator.calculate(input, 0, 0);
- int x = calculator.calculate(input, 1, 0) - z;
- int y = calculator.calculate(input, 0, 1) - z;
- // we need x*a + y*b + z == answer
- for (int a = 0; a <= 99; ++a) {
- int rem = goal - x*a - z;
- if (rem%y == 0) {
- int b = rem/y;
- if (b >= 0 && b <= 99) {
- ans = a*100 + b;
- break;
- }
- }
- }
- return ans;
- }
- }
- public class Main {
- private static List <Integer> parseInput() {
- List <Integer> answer = new ArrayList <Integer> ();
- try {
- Scanner scanner = new Scanner(new File("input.txt"));
- if (scanner.hasNextLine()) {
- String input = scanner.nextLine();
- for (String num : input.split(",")) {
- answer.add(Integer.parseInt(num));
- }
- }
- else System.out.println("File data line missing!");
- } catch (FileNotFoundException e) {
- System.out.println("File missing!");
- }
- return answer;
- }
- public static void main(String[] args) {
- AOC1 solver = new AOC1();
- AOC2_2 solver2 = new AOC2_2();
- System.out.println(solver.solve(parseInput()));
- System.out.println(solver2.solve(parseInput()));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement