-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathDay8.java
41 lines (34 loc) · 1 KB
/
Day8.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
package com.sbaars.adventofcode.year20.days;
import com.sbaars.adventofcode.year20.Day2020;
import com.sbaars.adventofcode.year20.gamepad.Gamepad;
import java.util.HashSet;
import java.util.Set;
public class Day8 extends Day2020 {
public Day8() {
super(8);
}
public static void main(String[] args) {
new Day8().printParts();
}
@Override
public Object part1() {
Gamepad gamepad = new Gamepad(dayStream());
Set<Integer> visited = new HashSet<>();
while (visited.add(gamepad.executeInstruction())) ;
return gamepad.getAccumulator();
}
@Override
public Object part2() {
for (int i = 0; i < 223; i++) {
Gamepad gamepad = new Gamepad(dayStream());
gamepad.replaceInstruction("jmp", "nop", i);
Set<Integer> visited = new HashSet<>();
while (visited.add(gamepad.executeInstruction())) {
if (gamepad.getCurrent() == gamepad.getSize()) {
return gamepad.getAccumulator();
}
}
}
throw new IllegalStateException();
}
}