-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDay23.java
137 lines (107 loc) · 2.88 KB
/
Day23.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package aoc19;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import myutils19.IntCodeComputer;
import myutils19.StaticUtils;
public class Day23 {
private List<Long> initialProgram;
private final int computerCount = 50;
private final int natRegister = 255;
public Day23(File inputFile) {
initialProgram = StaticUtils.commaSeperatedLongFileToList(inputFile);
}
public long run1() {
IntCodeComputer[] computers = getComputers();
int targetIndex = 255;
long targetVal = 0;
boolean foundTarget = false;
while(!foundTarget) {
for(int i = 0; i < computerCount; i++) {
IntCodeComputer current = computers[i];
current.run();
if(current.isStandby()) {
current.setInputValues(-1);
current.run();
}
Queue<Long> output = current.outputValues();
while(!output.isEmpty()) {
long register = output.poll();
long x = output.poll();
long y = output.poll();
if(register == targetIndex) {
foundTarget = true;
targetVal = y;
break;
}
if(register < computerCount) {
computers[(int) register].setInputValues(x, y);
}
}
if(foundTarget) {
break;
}
}
}
return targetVal;
}
private long run2() {
IntCodeComputer[] computers = getComputers();
long natX = -1;
long natY = -1;
boolean foundTarget = false;
long regZeroY = -1;
while(!foundTarget) {
for(int i = 0; i < computerCount; i++) {
IntCodeComputer current = computers[i];
current.run();
if(current.isStandby()) {
current.setInputValues(-1);
current.run();
}
Queue<Long> output = current.outputValues();
while(!output.isEmpty()) {
long register = output.poll();
long x = output.poll();
long y = output.poll();
if(register == natRegister) {
natX = x;
natY = y;
} else {
computers[(int) register].setInputValues(x, y);
}
}
}
if(computersAreIdle(computers)) {
if(natY == regZeroY) {
foundTarget = true;
}
computers[0].setInputValues(natX, natY);
regZeroY = natY;
}
}
return regZeroY;
}
private boolean computersAreIdle(IntCodeComputer[] computers) {
for(IntCodeComputer computer : computers) {
if(!computer.inputValues().isEmpty()) {
return false;
}
}
return true;
}
private IntCodeComputer[] getComputers() {
IntCodeComputer[] computers = new IntCodeComputer[computerCount];
for(int i = 0; i < computerCount; i++) {
IntCodeComputer comp = new IntCodeComputer(new ArrayList<>(initialProgram));
comp.setInputValues(i);
computers[i] = comp;
}
return computers;
}
public static void main(String[] args) {
Day23 test = new Day23(new File("C:\\Users\\Timucin\\Desktop\\Advent of code 2019\\Day 23\\InputFile.txt"));
System.out.println(test.run2());
}
}