-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDay16.java
380 lines (303 loc) · 11.5 KB
/
Day16.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package aoc18;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class Day16 {
private List<int[]> registersBefore;
private List<int[]> instructions;
private List<int[]> registersAfter;
// private Map<int[], List<String>> possibleOpsForInstructions;
public Day16(File inputFile) throws IOException {
registersBefore = new ArrayList<int[]>();
instructions = new ArrayList<int[]>();
registersAfter = new ArrayList<int[]>();
// possibleOpsForInstructions = new HashMap<int[], List<String>>();
fillLists(inputFile, registersBefore, instructions, registersAfter);
}
// @params regs array of 4 numbers. the registers.
// @params instr array of 4 numbers. the instructions (op code, A, B, C).
public static void addr(int[] regs, int[] instr) {
regs[instr[3]] = regs[instr[1]] + regs[instr[2]];
}
public static void addi(int[] regs, int[] instr) {
regs[instr[3]] = regs[instr[1]] + instr[2];
}
public static void mulr(int[] regs, int[] instr) {
regs[instr[3]] = regs[instr[1]] * regs[instr[2]];
}
public static void muli(int[] regs, int[] instr) {
regs[instr[3]] = regs[instr[1]] * instr[2];
}
// bitwise AND
public static void banr(int[] regs, int[] instr) {
regs[instr[3]] = regs[instr[1]] & regs[instr[2]];
}
public static void bani(int[] regs, int[] instr) {
regs[instr[3]] = regs[instr[1]] & instr[2];
}
// bitwise OR
public static void borr(int[] regs, int[] instr) {
regs[instr[3]] = regs[instr[1]] | regs[instr[2]];
}
public static void bori(int[] regs, int[] instr) {
regs[instr[3]] = regs[instr[1]] | instr[2];
}
// set value
public static void setr(int[] regs, int[] instr) {
regs[instr[3]] = regs[instr[1]];
}
public static void seti(int[] regs, int[] instr) {
regs[instr[3]] = instr[1];
}
public static void gtir(int[] regs, int[] instr) {
regs[instr[3]] = instr[1] > regs[instr[2]] ? 1 : 0;
}
public static void gtri(int[] regs, int[] instr) {
regs[instr[3]] = regs[instr[1]] > instr[2] ? 1 : 0;
}
public static void gtrr(int[] regs, int[] instr) {
regs[instr[3]] = regs[instr[1]] > regs[instr[2]] ? 1 : 0;
}
public static void eqir(int[] regs, int[] instr) {
regs[instr[3]] = instr[1] == regs[instr[2]] ? 1 : 0;
}
public static void eqri(int[] regs, int[] instr) {
regs[instr[3]] = regs[instr[1]] == instr[2] ? 1 : 0;
}
public static void eqrr(int[] regs, int[] instr) {
regs[instr[3]] = regs[instr[1]] == regs[instr[2]] ? 1 : 0;
}
// fills the lists from the given input file
private void fillLists(File input, List<int[]> registersBefore, List<int[]> instructions,
List<int[]> registersAfter) throws IOException {
Scanner sc = new Scanner(input);
String currentLine = "";
while (sc.hasNext()) {
if (((currentLine = sc.nextLine()).length() == 0))
continue;
if (currentLine.charAt(0) == 'B') {
int[] registerBefore = new int[4];
int registerIndex = 0;
String[] numsAsStringArray = currentLine
.substring(currentLine.indexOf('[') + 1, currentLine.indexOf(']')).trim().split("\\s*,\\s*");
;
for (String st : numsAsStringArray) {
registerBefore[registerIndex++] = Integer.parseInt(st);
}
registersBefore.add(registerBefore);
} else if (Character.isDigit(currentLine.charAt(0))) {
int[] instruction = new int[4];
int instructionIndex = 0;
String[] numsAsStringArray = currentLine.split(" ");
for (String st : numsAsStringArray) {
instruction[instructionIndex++] = Integer.parseInt(st);
}
instructions.add(instruction);
} else if (currentLine.charAt(0) == 'A') {
int[] registerAfter = new int[4];
int registerAfterIndex = 0;
String[] numsAsStringArray = currentLine
.substring(currentLine.indexOf('[') + 1, currentLine.indexOf(']')).trim().split("\\s*,\\s*");
;
for (String st : numsAsStringArray) {
registerAfter[registerAfterIndex++] = Integer.parseInt(st);
}
registersAfter.add(registerAfter);
}
}
sc.close();
}
// runs a test for a single instruction and saves the possible operations in
// a map for later evaluation
private void runTest(int[] registerBefore, int[] instruction, int[] registerAfter,
Map<int[], List<String>> possibleOpsForInstructions) {
int[] tmpRegister = registerBefore.clone();
List<String> possibleOps = new LinkedList<String>();
addi(tmpRegister, instruction);
if (Arrays.equals(tmpRegister, registerAfter))
possibleOps.add("addi");
tmpRegister = registerBefore.clone();
addr(tmpRegister, instruction);
if (Arrays.equals(tmpRegister, registerAfter))
possibleOps.add("addr");
tmpRegister = registerBefore.clone();
bani(tmpRegister, instruction);
if (Arrays.equals(tmpRegister, registerAfter))
possibleOps.add("bani");
tmpRegister = registerBefore.clone();
banr(tmpRegister, instruction);
if (Arrays.equals(tmpRegister, registerAfter))
possibleOps.add("banr");
tmpRegister = registerBefore.clone();
bori(tmpRegister, instruction);
if (Arrays.equals(tmpRegister, registerAfter))
possibleOps.add("bori");
tmpRegister = registerBefore.clone();
borr(tmpRegister, instruction);
if (Arrays.equals(tmpRegister, registerAfter))
possibleOps.add("borr");
tmpRegister = registerBefore.clone();
eqir(tmpRegister, instruction);
if (Arrays.equals(tmpRegister, registerAfter))
possibleOps.add("eqir");
tmpRegister = registerBefore.clone();
eqri(tmpRegister, instruction);
if (Arrays.equals(tmpRegister, registerAfter))
possibleOps.add("eqri");
tmpRegister = registerBefore.clone();
eqrr(tmpRegister, instruction);
if (Arrays.equals(tmpRegister, registerAfter))
possibleOps.add("eqrr");
tmpRegister = registerBefore.clone();
gtir(tmpRegister, instruction);
if (Arrays.equals(tmpRegister, registerAfter))
possibleOps.add("gtir");
tmpRegister = registerBefore.clone();
gtri(tmpRegister, instruction);
if (Arrays.equals(tmpRegister, registerAfter))
possibleOps.add("gtri");
tmpRegister = registerBefore.clone();
gtrr(tmpRegister, instruction);
if (Arrays.equals(tmpRegister, registerAfter))
possibleOps.add("gtrr");
tmpRegister = registerBefore.clone();
muli(tmpRegister, instruction);
if (Arrays.equals(tmpRegister, registerAfter))
possibleOps.add("muli");
tmpRegister = registerBefore.clone();
mulr(tmpRegister, instruction);
if (Arrays.equals(tmpRegister, registerAfter))
possibleOps.add("mulr");
tmpRegister = registerBefore.clone();
seti(tmpRegister, instruction);
if (Arrays.equals(tmpRegister, registerAfter))
possibleOps.add("seti");
tmpRegister = registerBefore.clone();
setr(tmpRegister, instruction);
if (Arrays.equals(tmpRegister, registerAfter))
possibleOps.add("setr");
possibleOpsForInstructions.putIfAbsent(instruction, possibleOps);
}
// part 1
public int run1() throws IOException {
int count = 0;
Map<int[], List<String>> possibleOpsForInstructions = getPossibleOperationsMap();
for (Map.Entry<int[], List<String>> entry : possibleOpsForInstructions.entrySet()) {
if (entry.getValue().size() >= 3)
count++;
}
return count;
}
// part 2
public int run2() throws IOException {
Map<Integer, String> opcodes = getOpcodes();
int[] mainRegisters = { 0, 0, 0, 0 };
List<int[]> instructions = getInstructions(
new File("C:\\Users\\Timucin\\Desktop\\Advent of code 2018\\Day 16\\InputFileSectionTwo.txt"));
for (int[] instruction : instructions) {
int opcode = instruction[0];
if (opcodes.get(opcode).equals("addi"))
addi(mainRegisters, instruction);
else if (opcodes.get(opcode).equals("addr"))
addr(mainRegisters, instruction);
else if (opcodes.get(opcode).equals("mulr"))
mulr(mainRegisters, instruction);
else if (opcodes.get(opcode).equals("muli"))
muli(mainRegisters, instruction);
else if (opcodes.get(opcode).equals("banr"))
banr(mainRegisters, instruction);
else if (opcodes.get(opcode).equals("bani"))
bani(mainRegisters, instruction);
else if (opcodes.get(opcode).equals("borr"))
borr(mainRegisters, instruction);
else if (opcodes.get(opcode).equals("bori"))
bori(mainRegisters, instruction);
else if (opcodes.get(opcode).equals("setr"))
setr(mainRegisters, instruction);
else if (opcodes.get(opcode).equals("seti"))
seti(mainRegisters, instruction);
else if (opcodes.get(opcode).equals("gtir"))
gtir(mainRegisters, instruction);
else if (opcodes.get(opcode).equals("gtri"))
gtri(mainRegisters, instruction);
else if (opcodes.get(opcode).equals("gtrr"))
gtrr(mainRegisters, instruction);
else if (opcodes.get(opcode).equals("eqir"))
eqir(mainRegisters, instruction);
else if (opcodes.get(opcode).equals("eqri"))
eqri(mainRegisters, instruction);
else if (opcodes.get(opcode).equals("eqrr"))
eqrr(mainRegisters, instruction);
}
return mainRegisters[0];
}
private List<int[]> getInstructions(File inputFile) throws IOException {
List<int[]> instructions = new ArrayList<int[]>();
Scanner sc = new Scanner(inputFile);
while (sc.hasNext()) {
String currentLine = sc.nextLine();
int[] instruction = new int[4];
int instructionIndex = 0;
String[] numsAsStringArray = currentLine.split(" ");
for (String st : numsAsStringArray) {
instruction[instructionIndex++] = Integer.parseInt(st);
}
instructions.add(instruction);
}
sc.close();
return instructions;
}
// returns a mapping of opcodes (integer 0 - 15) and the corresponding
// operation (addi, addr, mulr...)
private Map<Integer, String> getOpcodes() {
Map<int[], List<String>> possibleOpsForInstr = getPossibleOperationsMap();
Map<Integer, String> opcodes = new HashMap<Integer, String>();
int totalNumOfOpcodes = 16;
while (opcodes.size() != totalNumOfOpcodes) {
int[] targetInstruction = null;
String targetOperation = "";
// search for an instruction that only has a single corresponding
// operation
for (Map.Entry<int[], List<String>> entry : possibleOpsForInstr.entrySet()) {
if (entry.getValue().size() == 1 && !opcodes.containsKey(entry.getKey()[0])) {
targetInstruction = entry.getKey();
targetOperation = entry.getValue().get(0);
break;
}
}
// map the operation to an opcode
if (targetInstruction != null)
opcodes.putIfAbsent(targetInstruction[0], targetOperation);
// delete the operation from every list as it's already mapped to an
// opcode
for (Map.Entry<int[], List<String>> entry : possibleOpsForInstr.entrySet()) {
if (entry.getValue().contains(targetOperation)) {
entry.getValue().remove(targetOperation);
}
}
}
return opcodes;
}
// returns a mapping of instructions and possible operations
private Map<int[], List<String>> getPossibleOperationsMap() {
Map<int[], List<String>> possibleOpsForInstructions = new HashMap<int[], List<String>>();
int numOfSamples = registersBefore.size();
for (int i = 0; i < numOfSamples; i++) {
runTest(registersBefore.get(i), instructions.get(i), registersAfter.get(i), possibleOpsForInstructions);
}
return possibleOpsForInstructions;
}
public static void main(String[] args) throws IOException {
Day16 test = new Day16(
new File("C:\\Users\\Timucin\\Desktop\\Advent of code 2018\\Day 16\\InputFileSectionOne.txt"));
int resultPart2 = test.run2();
// int resultPart1 = test.run1();
System.out.println(resultPart2);
}
}