-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDay11.java
282 lines (237 loc) · 8.72 KB
/
Day11.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
package aoc16;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Queue;
import java.util.Set;
import myutils16.StaticUtils;
public class Day11 {
List<String> rawData;
public Day11(File input) {
rawData = StaticUtils.inputToList(input);
}
// part2Flag == true for solution for part 2
// part 2 takes around 30 seconds to run. might optimize later but for now, I
// think it's fine given the constraints
public int run(boolean part2Flag) {
List<Integer> initialFloorStates = getInitialState();
int finalBitCount = initialFloorStates.stream().mapToInt(n -> Integer.bitCount(n)).sum();
if (part2Flag) {
int floor1 = initialFloorStates.get(0) | (0b1111 << finalBitCount);
initialFloorStates.set(0, floor1);
finalBitCount += 4;
}
// floor indexing starts at 0
AreaState initialState = new AreaState(initialFloorStates, 0, 0);
Queue<AreaState> queue = new LinkedList<>();
queue.add(initialState);
Set<AreaState> visitedStates = new HashSet<>();
while (!queue.isEmpty()) {
AreaState currentState = queue.poll();
List<Integer> currentFloorStates = currentState.state;
int currentSteps = currentState.stepsTaken;
int currentFloor = currentState.currentFloor;
// done
if (Integer.bitCount(currentFloorStates.get(3)) == finalBitCount) {
return currentSteps;
}
List<Integer> equipmentIndicies = getOneBitIndicies(currentFloorStates.get(currentFloor), finalBitCount);
// add every possible move to the queue
// can go up
int currentFloorState = currentFloorStates.get(currentFloor);
if (currentFloor < 3) {
int nextFloor = currentFloor + 1;
int nextFloorState = currentFloorStates.get(nextFloor);
// take one at a time
for (int index : equipmentIndicies) {
int nextStateCurrentFloor = clearBit(currentFloorState, index);
int nextStateNextFloor = setBit(nextFloorState, index);
// next state is valid
if (isSafeToMove(finalBitCount, nextStateCurrentFloor)
&& isSafeToMove(finalBitCount, nextStateNextFloor)) {
List<Integer> nextFloorStates = new ArrayList<>(currentFloorStates);
nextFloorStates.set(currentFloor, nextStateCurrentFloor);
nextFloorStates.set(nextFloor, nextStateNextFloor);
AreaState nextState = new AreaState(nextFloorStates, currentSteps + 1, currentFloor + 1);
if (!visitedStates.contains(nextState)) {
queue.add(nextState);
visitedStates.add(nextState);
}
}
}
// take two at a time
for (int i = 0; i < equipmentIndicies.size() - 1; i++) {
for (int j = i + 1; j < equipmentIndicies.size(); j++) {
int nextStateCurrentFloor = clearBit(currentFloorState, equipmentIndicies.get(i));
nextStateCurrentFloor = clearBit(nextStateCurrentFloor, equipmentIndicies.get(j));
int nextStateNextFloor = setBit(nextFloorState, equipmentIndicies.get(i));
nextStateNextFloor = setBit(nextStateNextFloor, equipmentIndicies.get(j));
if (isSafeToMove(finalBitCount, nextStateCurrentFloor)
&& isSafeToMove(finalBitCount, nextStateNextFloor)) {
List<Integer> nextFloorStates = new ArrayList<>(currentFloorStates);
nextFloorStates.set(currentFloor, nextStateCurrentFloor);
nextFloorStates.set(nextFloor, nextStateNextFloor);
AreaState nextState = new AreaState(nextFloorStates, currentSteps + 1, currentFloor + 1);
if (!visitedStates.contains(nextState)) {
queue.add(nextState);
visitedStates.add(nextState);
}
}
}
}
}
// can go down
if (currentFloor > 0) {
int nextFloor = currentFloor - 1;
int nextFloorState = currentFloorStates.get(nextFloor);
// take one at a time
for (int index : equipmentIndicies) {
int nextStateCurrentFloor = clearBit(currentFloorState, index);
int nextStateNextFloor = setBit(nextFloorState, index);
// next state is valid
if (isSafeToMove(finalBitCount, nextStateCurrentFloor)
&& isSafeToMove(finalBitCount, nextStateNextFloor)) {
List<Integer> nextFloorStates = new ArrayList<>(currentFloorStates);
nextFloorStates.set(currentFloor, nextStateCurrentFloor);
nextFloorStates.set(nextFloor, nextStateNextFloor);
AreaState nextState = new AreaState(nextFloorStates, currentSteps + 1, currentFloor - 1);
if (!visitedStates.contains(nextState)) {
queue.add(nextState);
visitedStates.add(nextState);
}
}
}
// take two at a time
for (int i = 0; i < equipmentIndicies.size() - 1; i++) {
for (int j = i + 1; j < equipmentIndicies.size(); j++) {
int nextStateCurrentFloor = clearBit(currentFloorState, equipmentIndicies.get(i));
nextStateCurrentFloor = clearBit(nextStateCurrentFloor, equipmentIndicies.get(j));
int nextStateNextFloor = setBit(nextFloorState, equipmentIndicies.get(i));
nextStateNextFloor = setBit(nextStateNextFloor, equipmentIndicies.get(j));
if (isSafeToMove(finalBitCount, nextStateCurrentFloor)
&& isSafeToMove(finalBitCount, nextStateNextFloor)) {
List<Integer> nextFloorStates = new ArrayList<>(currentFloorStates);
nextFloorStates.set(currentFloor, nextStateCurrentFloor);
nextFloorStates.set(nextFloor, nextStateNextFloor);
AreaState nextState = new AreaState(nextFloorStates, currentSteps + 1, currentFloor - 1);
if (!visitedStates.contains(nextState)) {
queue.add(nextState);
visitedStates.add(nextState);
}
}
}
}
}
}
return -1;
}
// check if the next floor state is valid
private boolean isSafeToMove(int bitCount, int nextState) {
boolean hasExposedChip = false;
boolean hasGenerator = false;
for (int i = 0; i < bitCount; i++) {
if (i % 2 == 0) {
if (getBit(nextState, i) == 1 && getBit(nextState, i + 1) == 0) {
hasExposedChip = true;
if (hasGenerator) {
return false;
}
}
} else if (i % 2 != 0 && getBit(nextState, i) == 1) {
hasGenerator = true;
if (hasExposedChip) {
return false;
}
}
}
return true;
}
// get the initial state of the floors as a list of 4 integers. Each bit
// represents a generator or a microchip. (generators left, chips right)
private List<Integer> getInitialState() {
List<Integer> initialState = new ArrayList<>();
Map<String, Integer> indexTable = new HashMap<>();
int indexIncr = -2;
for (String line : rawData) {
int floorState = 0;
// empty floor
if (line.contains("nothing")) {
initialState.add(floorState);
continue;
}
String[] equipment = Arrays
.stream(line.substring(line.indexOf("contains") + "contains".length() + 1).split(",|and"))
.filter(s -> !s.isBlank()).map(String::trim).toArray(String[]::new);
for (String equipName : equipment) {
String equipTmp = equipName.contains("-") ? equipName.substring(0, equipName.indexOf('-'))
: equipName.substring(0, equipName.lastIndexOf(' '));
if (!indexTable.containsKey(equipTmp)) {
indexIncr += 2;
indexTable.put(equipTmp, indexIncr);
}
if (equipName.contains("generator")) {
floorState |= (1 << (indexTable.get(equipTmp) + 1));
} else {
floorState |= (1 << indexTable.get(equipTmp));
}
}
initialState.add(floorState);
}
return initialState;
}
public static void main(String[] args) {
Day11 test = new Day11(new File("\\C:\\Users\\Timucin\\Desktop\\Advent of code 2016\\Day 11\\InputFile1.txt"));
System.out.println(test.run(true));
}
private int getBit(int num, int position) {
return (num >> position) & 1;
}
private int setBit(int num, int position) {
return num | (1 << position);
}
private int clearBit(int num, int position) {
return num & ~(1 << position);
}
// returns a list of indicies corresponding to the positions (from right to
// left) of the bits set to 1
private List<Integer> getOneBitIndicies(int num, int len) {
List<Integer> indicies = new ArrayList<>();
for (int i = 0; i < len; i++) {
if (getBit(num, i) == 1) {
indicies.add(i);
}
}
return indicies;
}
private static class AreaState {
public List<Integer> state;
public int stepsTaken;
public int currentFloor;
public AreaState(List<Integer> state, int stepsTaken, int currentFloor) {
this.state = state;
this.stepsTaken = stepsTaken;
this.currentFloor = currentFloor;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || !(o instanceof AreaState)) {
return false;
}
AreaState tmp = (AreaState) o;
return this.currentFloor == tmp.currentFloor && this.state.equals(tmp.state);
}
@Override
public int hashCode() {
return Objects.hash(currentFloor, state);
}
}
}