-
Notifications
You must be signed in to change notification settings - Fork 0
/
AoC2019_02.java
59 lines (48 loc) · 1.78 KB
/
AoC2019_02.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import static com.github.pareronia.aoc.IntegerSequence.Range.range;
import static com.github.pareronia.aoc.IterTools.productIterator;
import java.util.ArrayList;
import java.util.List;
import com.github.pareronia.aoc.Utils;
import com.github.pareronia.aoc.intcode.IntCode;
import com.github.pareronia.aoc.solution.SolutionBase;
public class AoC2019_02 extends SolutionBase<List<Long>, Long, Integer> {
private AoC2019_02(final boolean debug) {
super(debug);
}
public static AoC2019_02 create() {
return new AoC2019_02(false);
}
public static AoC2019_02 createDebug() {
return new AoC2019_02(true);
}
private Long runProgram(
final List<Long> program,
final long noun,
final long verb
) {
final List<Long> theProgram = new ArrayList<>(program);
theProgram.set(1, noun);
theProgram.set(2, verb);
final IntCode intCode = new IntCode(theProgram, this.debug);
intCode.run(theProgram);
return intCode.getProgram().get(0);
}
@Override
protected List<Long> parseInput(final List<String> inputs) {
return IntCode.parse(inputs.get(0));
}
@Override
public Long solvePart1(final List<Long> program) {
return runProgram(program, 12, 2);
}
@Override
public Integer solvePart2(final List<Long> program) {
return Utils.stream(productIterator(range(100), range(100)))
.filter(p -> runProgram(program, p.first(), p.second()) == 19_690_720)
.map(p -> 100 * p.first() + p.second())
.findFirst().orElseThrow();
}
public static void main(final String[] args) throws Exception {
AoC2019_02.create().run();
}
}