-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathDay16.java
166 lines (147 loc) · 5.23 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
package com.sbaars.adventofcode.year18.days;
import com.sbaars.adventofcode.year18.Day2018;
import java.util.*;
import java.util.function.*;
public class Day16 extends Day2018 {
private record Sample(int[] before, int[] instruction, int[] after) {}
private final List<BiFunction<int[], int[], Integer>> opcodes = Arrays.asList(
// addr
(args, reg) -> reg[args[0]] + reg[args[1]],
// addi
(args, reg) -> reg[args[0]] + args[1],
// mulr
(args, reg) -> reg[args[0]] * reg[args[1]],
// muli
(args, reg) -> reg[args[0]] * args[1],
// banr
(args, reg) -> reg[args[0]] & reg[args[1]],
// bani
(args, reg) -> reg[args[0]] & args[1],
// borr
(args, reg) -> reg[args[0]] | reg[args[1]],
// bori
(args, reg) -> reg[args[0]] | args[1],
// setr
(args, reg) -> reg[args[0]],
// seti
(args, reg) -> args[0],
// gtir
(args, reg) -> args[0] > reg[args[1]] ? 1 : 0,
// gtri
(args, reg) -> reg[args[0]] > args[1] ? 1 : 0,
// gtrr
(args, reg) -> reg[args[0]] > reg[args[1]] ? 1 : 0,
// eqir
(args, reg) -> args[0] == reg[args[1]] ? 1 : 0,
// eqri
(args, reg) -> reg[args[0]] == args[1] ? 1 : 0,
// eqrr
(args, reg) -> reg[args[0]] == reg[args[1]] ? 1 : 0
);
public Day16() {
super(16);
}
public static void main(String[] args) {
new Day16().printParts();
}
private List<Sample> parseSamples() {
List<Sample> samples = new ArrayList<>();
String[] lines = day().split("\n");
for (int i = 0; i < lines.length; i++) {
String line = lines[i].trim();
if (line.startsWith("Before:")) {
int[] before = Arrays.stream(line.substring(line.indexOf("[") + 1, line.indexOf("]"))
.split(",")).map(String::trim).mapToInt(Integer::parseInt).toArray();
int[] instruction = Arrays.stream(lines[i + 1].trim().split(" "))
.mapToInt(Integer::parseInt).toArray();
int[] after = Arrays.stream(lines[i + 2].trim()
.substring(lines[i + 2].indexOf("[") + 1, lines[i + 2].indexOf("]"))
.split(",")).map(String::trim).mapToInt(Integer::parseInt).toArray();
samples.add(new Sample(before, instruction, after));
i += 3;
}
if (line.isEmpty() && i + 1 < lines.length && !lines[i + 1].contains("[")) {
break;
}
}
return samples;
}
private List<int[]> parseTestProgram() {
String[] lines = day().split("\n");
boolean testProgram = false;
List<int[]> program = new ArrayList<>();
for (String line : lines) {
line = line.trim();
if (line.isEmpty()) {
testProgram = true;
continue;
}
if (testProgram && !line.contains("[")) {
program.add(Arrays.stream(line.split(" ")).mapToInt(Integer::parseInt).toArray());
}
}
return program;
}
private boolean testOpcode(Sample sample, BiFunction<int[], int[], Integer> operation) {
int[] registers = sample.before.clone();
int a = sample.instruction[1];
int b = sample.instruction[2];
int c = sample.instruction[3];
registers[c] = operation.apply(new int[]{a, b}, registers);
return Arrays.equals(registers, sample.after);
}
private Map<Integer, BiFunction<int[], int[], Integer>> determineOpcodes(List<Sample> samples) {
Map<Integer, Set<Integer>> possibleOpcodes = new HashMap<>();
Map<Integer, BiFunction<int[], int[], Integer>> finalOpcodes = new HashMap<>();
// Find all possible opcodes for each number
for (Sample sample : samples) {
int opcodeNum = sample.instruction[0];
possibleOpcodes.putIfAbsent(opcodeNum, new HashSet<>());
for (int i = 0; i < opcodes.size(); i++) {
if (testOpcode(sample, opcodes.get(i))) {
possibleOpcodes.get(opcodeNum).add(i);
}
}
}
// Eliminate possibilities until each number has exactly one opcode
while (possibleOpcodes.size() > 0) {
for (Map.Entry<Integer, Set<Integer>> entry : possibleOpcodes.entrySet()) {
if (entry.getValue().size() == 1) {
int opcodeNum = entry.getKey();
int opcodeIndex = entry.getValue().iterator().next();
finalOpcodes.put(opcodeNum, opcodes.get(opcodeIndex));
// Remove this opcode from all other possibilities
possibleOpcodes.values().forEach(set -> set.remove(opcodeIndex));
possibleOpcodes.remove(opcodeNum);
break;
}
}
}
return finalOpcodes;
}
@Override
public Object part1() {
List<Sample> samples = parseSamples();
return samples.stream()
.map(sample -> opcodes.stream()
.filter(op -> testOpcode(sample, op))
.count())
.filter(count -> count >= 3)
.count();
}
@Override
public Object part2() {
List<Sample> samples = parseSamples();
Map<Integer, BiFunction<int[], int[], Integer>> opcodeMap = determineOpcodes(samples);
List<int[]> program = parseTestProgram();
int[] registers = new int[4];
for (int[] instruction : program) {
int opcode = instruction[0];
int a = instruction[1];
int b = instruction[2];
int c = instruction[3];
registers[c] = opcodeMap.get(opcode).apply(new int[]{a, b}, registers);
}
return registers[0];
}
}