Day02 2019
This commit is contained in:
parent
4a90257257
commit
4fef41464e
@ -36,13 +36,12 @@ public class Day01 {
|
||||
var total = 0;
|
||||
for (var mass : input) {
|
||||
int fuel = fuel(mass);
|
||||
int subTotal = fuel;
|
||||
total += fuel;
|
||||
while (true) {
|
||||
fuel = fuel(fuel);
|
||||
if (fuel <= 0) break;
|
||||
subTotal += fuel;
|
||||
total += fuel;
|
||||
}
|
||||
total += subTotal;
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
44
2019/src/main/java/be/vandewalleh/aoc/Day02.java
Normal file
44
2019/src/main/java/be/vandewalleh/aoc/Day02.java
Normal file
@ -0,0 +1,44 @@
|
||||
package be.vandewalleh.aoc;
|
||||
|
||||
import be.vandewalleh.aoc.intcode.IntCodeInterpreter;
|
||||
import be.vandewalleh.aoc.utils.factory.Days;
|
||||
import be.vandewalleh.aoc.utils.input.Csv;
|
||||
import be.vandewalleh.aoc.utils.input.Day;
|
||||
import be.vandewalleh.aoc.utils.input.Input;
|
||||
|
||||
@Day(2)
|
||||
public class Day02 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
var day = Days.createDay(Day02.class);
|
||||
System.out.println(day.part1());
|
||||
System.out.println(day.part2());
|
||||
}
|
||||
|
||||
private final int[] input;
|
||||
|
||||
public Day02(@Csv Input<int[]> input) {
|
||||
this.input = input.getValue();
|
||||
}
|
||||
|
||||
private long runInterpreterWith(int noun, int verb) {
|
||||
var interpreter = new IntCodeInterpreter(input);
|
||||
interpreter.setNoun(noun);
|
||||
interpreter.setVerb(verb);
|
||||
interpreter.run();
|
||||
return interpreter.getOutput();
|
||||
}
|
||||
|
||||
public long part1() {
|
||||
return runInterpreterWith(12, 2);
|
||||
}
|
||||
|
||||
public int part2() {
|
||||
for (var noun = 0; noun < 99; noun++) {
|
||||
for (var verb = 0; verb < 99; verb++) {
|
||||
if (runInterpreterWith(noun, verb) == 19690720L) return 100 * noun + verb;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,48 @@
|
||||
package be.vandewalleh.aoc.intcode;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class IntCodeInterpreter {
|
||||
private final long[] memory;
|
||||
|
||||
public IntCodeInterpreter(long[] memory) {
|
||||
var copy = new long[memory.length];
|
||||
System.arraycopy(memory, 0, copy, 0, memory.length);
|
||||
this.memory = copy;
|
||||
}
|
||||
|
||||
public IntCodeInterpreter(int[] memory) {
|
||||
this.memory = Arrays.stream(memory).asLongStream().toArray();
|
||||
}
|
||||
|
||||
public void setNoun(long noun) {
|
||||
memory[1] = noun;
|
||||
}
|
||||
|
||||
public void setVerb(long verb) {
|
||||
memory[2] = verb;
|
||||
}
|
||||
|
||||
public long getOutput() {
|
||||
return memory[0];
|
||||
}
|
||||
|
||||
public void run() {
|
||||
var pointer = 0;
|
||||
|
||||
loop:
|
||||
while (true) {
|
||||
var optionalOpCode = OpCode.from((int) memory[pointer]);
|
||||
var opCode = optionalOpCode.orElseThrow(() -> new IllegalArgumentException("Invalid OpCode"));
|
||||
var args = Arrays.copyOfRange(memory, pointer + 1, pointer + opCode.params + 1);
|
||||
switch (opCode) {
|
||||
case Add -> memory[(int) args[2]] = memory[(int) args[0]] + memory[(int) args[1]];
|
||||
case Multiply -> memory[(int) args[2]] = memory[(int) args[0]] * memory[(int) args[1]];
|
||||
case Halt -> {
|
||||
break loop;
|
||||
}
|
||||
}
|
||||
pointer += opCode.params + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
24
2019/src/main/java/be/vandewalleh/aoc/intcode/OpCode.java
Normal file
24
2019/src/main/java/be/vandewalleh/aoc/intcode/OpCode.java
Normal file
@ -0,0 +1,24 @@
|
||||
package be.vandewalleh.aoc.intcode;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public enum OpCode {
|
||||
Add(1, 3),
|
||||
Multiply(2, 3),
|
||||
Halt(99, 0);
|
||||
|
||||
public final int value;
|
||||
public final int params;
|
||||
|
||||
OpCode(int value, int params) {
|
||||
this.value = value;
|
||||
this.params = params;
|
||||
}
|
||||
|
||||
public static Optional<OpCode> from(int value) {
|
||||
for (OpCode opCode : OpCode.values()) {
|
||||
if (opCode.value == value) return Optional.of(opCode);
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
1
2019/src/main/resources/day02.txt
Normal file
1
2019/src/main/resources/day02.txt
Normal file
@ -0,0 +1 @@
|
||||
1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,6,1,19,1,5,19,23,2,6,23,27,1,27,5,31,2,9,31,35,1,5,35,39,2,6,39,43,2,6,43,47,1,5,47,51,2,9,51,55,1,5,55,59,1,10,59,63,1,63,6,67,1,9,67,71,1,71,6,75,1,75,13,79,2,79,13,83,2,9,83,87,1,87,5,91,1,9,91,95,2,10,95,99,1,5,99,103,1,103,9,107,1,13,107,111,2,111,10,115,1,115,5,119,2,13,119,123,1,9,123,127,1,5,127,131,2,131,6,135,1,135,5,139,1,139,6,143,1,143,6,147,1,2,147,151,1,151,5,0,99,2,14,0,0
|
||||
Loading…
x
Reference in New Issue
Block a user