-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDay18.java
303 lines (247 loc) · 8.4 KB
/
Day18.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package aoc17;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Map;
import java.util.Queue;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.ArrayList;
public class Day18 {
private Map<Character, Long> registers;
private List<String> instructions;
public Day18(File input) {
instructions = getInstructions(input);
registers = getRegisters();
}
// part 2
public int numOfSndCmdProgram1() {
int count = 0;
Map<Character, Long> program1 = new HashMap<>();
Map<Character, Long> program2 = new HashMap<>();
for (Map.Entry<Character, Long> e : registers.entrySet()) {
program1.putIfAbsent(e.getKey(), 0L);
program2.putIfAbsent(e.getKey(), 0L);
}
program2.put('p', 1L);
// if both counters reach 4, the program ends.
int deadLockCounterP1 = 0;
int deadLockCounterP2 = 0;
// current indicies for program 1 and 2
int indexP1 = 0;
int indexP2 = 0;
// received values for program 1 and 2 (program 1 sends values to queue
// 2, program 2 sends values to queue 1)
Queue<Long> rcvP1 = new LinkedList<>();
Queue<Long> rcvP2 = new LinkedList<>();
int indexP1Before = 0;
int indexP2Before = 0;
while ((indexInRange(indexP1, instructions.size()) || (indexInRange(indexP2, instructions.size())))) {
String instructionP1 = null;
String instructionP2 = null;
if (indexInRange(indexP1, instructions.size())) {
instructionP1 = instructions.get(indexP1);
indexP1 += executeInstruction(instructionP1, program1, rcvP2, rcvP1);
}
int sndCountP1 = rcvP2.size();
if (indexInRange(indexP2, instructions.size())) {
instructionP2 = instructions.get(indexP2);
indexP2 += executeInstruction(instructionP2, program2, rcvP1, rcvP2);
}
// check if program 2 sent a value to program 1
if (sndCountP1 != rcvP2.size())
count++;
// check if programs are in deadlock
if (indexP1Before == indexP1)
deadLockCounterP1++;
else
deadLockCounterP1 = 0;
if (indexP2Before == indexP2)
deadLockCounterP2++;
else
deadLockCounterP2 = 0;
if (deadLockCounterP1 >= 4 && deadLockCounterP2 >= 4)
break;
indexP1Before = indexP1;
indexP2Before = indexP2;
}
return count;
}
private long executeInstruction(String instruction, Map<Character, Long> registers, Queue<Long> rcvValues,
Queue<Long> sndValues) {
String cmd = instruction.substring(0, 3);
char register = instruction.charAt(4);
if (cmd.equals("snd")) {
sndValues.offer(snd(register, registers));
} else if (cmd.equals("set")) {
set(register, instruction.substring(6), registers);
} else if (cmd.equals("add")) {
add(register, instruction.substring(6), registers);
} else if (cmd.equals("mul")) {
mul(register, instruction.substring(6), registers);
} else if (cmd.equals("mod")) {
mod(register, instruction.substring(6), registers);
} else if (cmd.equals("rcv")) {
if (rcvValues.isEmpty()) {
return 0;
}
registers.put(register, rcvValues.poll());
} else if (cmd.equals("jgz")) {
Long offset = jgz(register, instruction.substring(6), registers);
if (offset == null)
return 1;
else
return offset;
}
return 1;
}
private static boolean indexInRange(int index, int maxSize) {
return index >= 0 && index < maxSize;
}
// part 1
public long firstRcvValue() {
int firstRcv = 0;
long lastSound = 0;
int index = 0;
while (index < instructions.size() && index >= 0) {
String instruction = instructions.get(index);
String cmd = instruction.substring(0, 3);
char register = instruction.charAt(4);
if (cmd.equals("snd")) {
lastSound = snd(register, registers);
} else if (cmd.equals("set")) {
set(register, instruction.substring(6), registers);
} else if (cmd.equals("add")) {
add(register, instruction.substring(6), registers);
} else if (cmd.equals("mul")) {
mul(register, instruction.substring(6), registers);
} else if (cmd.equals("mod")) {
mod(register, instruction.substring(6), registers);
} else if (cmd.equals("rcv")) {
Long rcvVal = rcv(register, lastSound, registers);
if (rcvVal != null)
return lastSound;
} else if (cmd.equals("jgz")) {
Long offset = jgz(register, instruction.substring(6), registers);
if (offset == null)
index++;
else
index += offset;
continue;
}
index++;
}
return firstRcv;
}
private List<String> getInstructions(File input) {
List<String> instructions = new ArrayList<>();
try {
BufferedReader br = new BufferedReader(new FileReader(input));
String line = "";
while ((line = br.readLine()) != null) {
instructions.add(line);
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
return instructions;
}
public static void set(char x, String y, Map<Character, Long> registers) {
Long value = isNumeric(y) ? Long.parseLong(y) : null;
if (value == null) {
value = registers.get(y.charAt(0));
}
// System.out.println("set: " + value);
registers.put(x, value);
}
private static void add(char x, String y, Map<Character, Long> registers) {
Long value = isNumeric(y) ? Long.parseLong(y) : null;
if (value == null) {
value = registers.get(y.charAt(0));
}
// System.out.println("add: " + value);
registers.put(x, registers.get(x) + value);
}
public static void sub(char x, String y, Map<Character, Long> registers) {
Long value = isNumeric(y) ? Long.parseLong(y) : null;
if (value == null) {
value = registers.get(y.charAt(0));
}
// System.out.println("add: " + value);
registers.put(x, registers.get(x) - value);
}
public static Long jnz(char x, String y, Map<Character, Long> registers) {
// check if offset value is a register or a value
Long value = isNumeric(y) ? Long.parseLong(y) : null;
if (value == null) {
value = registers.get(y.charAt(0));
}
// check if instruction takes a register or a value
Long registerValue = Character.isDigit(x) ? Long.parseLong(Character.toString(x)) : null;
if (registerValue == null) {
return registers.get(x) != 0 ? value : null;
}
// System.out.println("jgz: " + value + " " + x);
return registerValue != 0 ? value : null;
}
public static void mul(char x, String y, Map<Character, Long> registers) {
Long value = isNumeric(y) ? Long.parseLong(y) : null;
if (value == null) {
value = registers.get(y.charAt(0));
}
// System.out.println("mul: " + value);
registers.put(x, registers.get(x) * value);
}
private static void mod(char x, String y, Map<Character, Long> registers) {
Long value = isNumeric(y) ? Long.parseLong(y) : null;
if (value == null) {
value = registers.get(y.charAt(0));
}
// System.out.println("mod: " + value);
registers.put(x, registers.get(x) % value);
}
private static Long rcv(char x, long lastPlayedSound, Map<Character, Long> registers) {
// System.out.println("rcv: " + registers.get(x));
return registers.get(x) != 0 ? lastPlayedSound : null;
}
private static Long jgz(char x, String y, Map<Character, Long> registers) {
// check if offset value is a register or a value
Long value = isNumeric(y) ? Long.parseLong(y) : null;
if (value == null) {
value = registers.get(y.charAt(0));
}
// check if instruction takes a register or a value
Long registerValue = Character.isDigit(x) ? Long.parseLong(Character.toString(x)) : null;
if (registerValue == null) {
return registers.get(x) > 0 ? value : null;
}
// System.out.println("jgz: " + value + " " + x);
return registerValue > 0 ? value : null;
}
private static long snd(char x, Map<Character, Long> registers) {
return Character.isDigit(x) ? Integer.parseInt(Character.toString(x)) : registers.get(x);
}
public static boolean isNumeric(String str) {
return str.matches("-?\\d+(\\.\\d+)?");
}
private Map<Character, Long> getRegisters() {
if (instructions.isEmpty())
return null;
Map<Character, Long> registers = new HashMap<>();
for (String instruction : instructions) {
char register = instruction.charAt(4);
if (!Character.isDigit(register))
registers.putIfAbsent(register, 0L);
}
return registers;
}
public static void main(String[] args) {
Day18 test = new Day18(new File("C:\\Users\\Timucin\\Desktop\\Advent of code 2017\\Day 18\\InputFile1.txt"));
// long res1 = test.firstRcvValue();
int res2 = test.numOfSndCmdProgram1();
System.out.println(res2);
}
}