-
Notifications
You must be signed in to change notification settings - Fork 0
/
AoC2016_11.java
389 lines (346 loc) Β· 15 KB
/
AoC2016_11.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
381
382
383
384
385
386
387
388
389
import static com.github.pareronia.aoc.AssertUtils.unreachable;
import static com.github.pareronia.aoc.IterTools.combinations;
import static java.util.Collections.emptyList;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.toList;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import com.github.pareronia.aoc.StringUtils;
import com.github.pareronia.aoc.solution.Sample;
import com.github.pareronia.aoc.solution.Samples;
import com.github.pareronia.aoc.solution.SolutionBase;
public final class AoC2016_11
extends SolutionBase<AoC2016_11.State, Integer, Integer> {
private AoC2016_11(final boolean debug) {
super(debug);
}
public static AoC2016_11 create() {
return new AoC2016_11(false);
}
public static AoC2016_11 createDebug() {
return new AoC2016_11(true);
}
@Override
protected State parseInput(final List<String> inputs) {
return State.fromInput(inputs);
}
private int solve(final State initialState) {
final Deque<Step> steps = new ArrayDeque<>();
steps.add(Step.of(0, initialState));
final Set<Integer> seen = new HashSet<>();
seen.add(initialState.equivalentState());
while (!steps.isEmpty()) {
final Step step = steps.poll();
final State state = step.state;
if (state.isDestination()) {
log("#steps: " + step.numberOfSteps);
return step.numberOfSteps;
}
state.moves().stream()
.filter(m -> !seen.contains(m.equivalentState()))
.forEach(m -> {
seen.add(m.equivalentState());
steps.add(Step.of(step.numberOfSteps + 1, m));
});
}
throw unreachable();
}
@Override
public Integer solvePart1(final State initialState) {
return solve(initialState);
}
@Override
public Integer solvePart2(final State initialState) {
final Map<String, Integer> chips = new HashMap<>(initialState.getChips());
chips.put("elerium", 1);
chips.put("dilithium", 1);
final Map<String, Integer> gennys = new HashMap<>(initialState.getGennys());
gennys.put("elerium", 1);
gennys.put("dilithium", 1);
return solve(initialState.withChips(chips).withGennys(gennys));
}
@Samples({
@Sample(method = "part1", input = TEST, expected = "11")
})
public static void main(final String[] args) throws Exception {
AoC2016_11.create().run();
}
private static final String TEST = """
The first floor contains a hydrogen-compatible microchip, and a lithium-compatible microchip.
The second floor contains a hydrogen generator.
The third floor contains a lithium generator.
The fourth floor contains nothing relevant.
""";
static final class State {
private static final List<Integer> FLOORS = List.of(1, 2, 3, 4);
private static final int TOP = FLOORS.stream().mapToInt(Integer::intValue).max().getAsInt();
private static final int BOTTOM = FLOORS.stream().mapToInt(Integer::intValue).min().getAsInt();
private static final int MAX_ITEMS_PER_MOVE = 2;
private final Integer elevator;
private final Map<String, Integer> chips;
private final Map<String, Integer> gennys;
private final Map<Integer, List<String>> chipsPerFloor;
private final Map<Integer, List<String>> gennysPerFloor;
private State(
final Integer elevator,
final Map<String, Integer> chips,
final Map<String, Integer> gennys,
final Map<Integer, List<String>> chipsPerFloor,
final Map<Integer, List<String>> gennysPerFloor
) {
this.elevator = elevator;
this.chips = chips;
this.gennys = gennys;
this.chipsPerFloor = chips.keySet().stream().collect(groupingBy(chips::get));
this.gennysPerFloor = gennys.keySet().stream().collect(groupingBy(gennys::get));
}
State(
final Integer elevator,
final Map<String, Integer> chips,
final Map<String, Integer> gennys
) {
this( elevator,
Collections.unmodifiableMap(chips),
Collections.unmodifiableMap(gennys),
null, null);
}
public static State fromInput(final List<String> inputs) {
final Map<String, Integer> chips = new HashMap<>();
final Map<String, Integer> generators = new HashMap<>();
for (int i = 0; i < inputs.size(); i++) {
String floor = inputs.get(i);
floor = floor.replaceAll(",? and", ",");
floor = floor.replace(".", "");
final String contains = floor.split(" contains ")[1];
final String[] contained = contains.split(", ");
for (final String containee : contained) {
final String[] s = containee.split(" ");
if ("nothing".equals(s[0])) {
continue;
} else if ("generator".equals(s[2])) {
generators.put(s[1], i + 1);
} else {
chips.put(StringUtils.substringBefore(s[1], "-"), i + 1);
}
}
}
return new State(1, chips, generators);
}
public Map<String, Integer> getChips() {
return chips;
}
public Map<String, Integer> getGennys() {
return gennys;
}
boolean isSafe() {
for (final Entry<String, Integer> chip : this.chips.entrySet()) {
final List<String> gennysOnSameFloor
= this.gennysPerFloor.get(chip.getValue());
if (gennysOnSameFloor != null
&& !gennysOnSameFloor.contains(chip.getKey())) {
return false;
}
}
return true;
}
public boolean isDestination() {
return this.elevator == TOP
&& this.chips.values().stream().allMatch(c -> c == TOP)
&& this.gennys.values().stream().allMatch(g -> g == TOP);
}
public Integer equivalentState() {
assert this.isSafe();
final StringBuilder eq = new StringBuilder();
eq.append(this.elevator);
for (final int f : FLOORS) {
eq.append(this.chipsPerFloor.getOrDefault(f, emptyList()).size());
eq.append(this.gennysPerFloor.getOrDefault(f, emptyList()).size());
}
return Integer.valueOf(eq.toString());
}
public List<State> moves() {
final List<State> states = new ArrayList<>();
final Integer floor = this.elevator;
final List<String> chipsOnFloor
= this.chipsPerFloor.getOrDefault(floor, emptyList());
final List<String> gennysOnFloor
= this.gennysPerFloor.getOrDefault(floor, emptyList());
states.addAll(moveMultipleChips(floor, chipsOnFloor));
states.addAll(moveSingleChips(floor, chipsOnFloor));
states.addAll(moveMultipleGennys(floor, gennysOnFloor));
states.addAll(moveSingleGennys(floor, gennysOnFloor));
states.addAll(moveChipAndGennyPairs(floor, chipsOnFloor, gennysOnFloor));
return states.stream()
.filter(s -> FLOORS.contains(s.elevator))
.filter(State::isSafe)
.collect(toList());
}
private List<State> moveMultipleChips(
final Integer floor,
final List<String> chipsOnFloor) {
final List<State> states = new ArrayList<>();
if (chipsOnFloor.size() >= MAX_ITEMS_PER_MOVE) {
combinations(chipsOnFloor.size(), MAX_ITEMS_PER_MOVE).forEach(
c -> {
final List<String> chipsToMove = Arrays.stream(c)
.mapToObj(chipsOnFloor::get).collect(toList());
states.add(moveUpWithChips(chipsToMove));
if (!floorsBelowEmpty(floor)) {
states.add(moveDownWithChips(chipsToMove));
}
});
}
return states;
}
private List<State> moveSingleChips(
final Integer floor,
final List<String> chipsOnFloor) {
final List<State> states = new ArrayList<>();
for (final String chip : chipsOnFloor) {
states.add(moveUpWithChips(List.of(chip)));
if (!floorsBelowEmpty(floor)) {
states.add(moveDownWithChips(List.of(chip)));
}
}
return states;
}
private List<State> moveMultipleGennys(
final Integer floor,
final List<String> gennysOnFloor) {
final List<State> states = new ArrayList<>();
if (gennysOnFloor.size() >= MAX_ITEMS_PER_MOVE) {
combinations(gennysOnFloor.size(), MAX_ITEMS_PER_MOVE).forEach(
c -> {
final List<String> gennysToMove = Arrays.stream(c)
.mapToObj(gennysOnFloor::get).collect(toList());
states.add(moveUpWitGennys(gennysToMove));
if (!floorsBelowEmpty(floor)) {
states.add(moveDownWithGennys(gennysToMove));
}
});
}
return states;
}
private List<State> moveSingleGennys(
final Integer floor,
final List<String> gennysOnFloor) {
final List<State> states = new ArrayList<>();
for (final String genny : gennysOnFloor) {
states.add(moveUpWitGennys(List.of(genny)));
if (!floorsBelowEmpty(floor)) {
states.add(moveDownWithGennys(List.of(genny)));
}
}
return states;
}
private List<State> moveChipAndGennyPairs(
final Integer floor,
final List<String> chipsOnFloor,
final List<String> gennysOnFloor) {
final List<State> states = new ArrayList<>();
final List<String> intersection = new ArrayList<>(chipsOnFloor);
intersection.retainAll(gennysOnFloor);
for (final String match : intersection) {
states.add(withChipsTo(List.of(match), floor + 1)
.withGennysTo(List.of(match), floor + 1)
.withElevator(floor + 1));
if (!floorsBelowEmpty(floor)) {
states.add(withChipsTo(List.of(match), floor - 1)
.withGennysTo(List.of(match), floor - 1)
.withElevator(floor - 1));
}
}
return states;
}
private State withElevator(final int elevator) {
return new State(elevator, this.chips, this.gennys);
}
private State withChips(final Map<String, Integer> chips) {
return new State(this.elevator, chips, this.gennys);
}
private State withGennys(final Map<String, Integer> gennys) {
return new State(this.elevator, this.chips, gennys);
}
private State moveUpWithChips(final List<String> chips) {
return withChipsTo(chips, this.elevator + 1)
.withElevator(this.elevator + 1);
}
private State moveUpWitGennys(final List<String> gennys) {
return withGennysTo(gennys, this.elevator + 1)
.withElevator(this.elevator + 1);
}
private State moveDownWithChips(final List<String> chips) {
return withChipsTo(chips, this.elevator - 1)
.withElevator(this.elevator - 1);
}
private State moveDownWithGennys(final List<String> gennys) {
return withGennysTo(gennys, this.elevator - 1)
.withElevator(this.elevator - 1);
}
private State withChipsTo(final List<String> chips, final Integer floor) {
final Map<String, Integer> newChips = new HashMap<>(this.chips);
chips.forEach(c -> newChips.put(c, floor));
return this.withChips(newChips);
}
private State withGennysTo(final List<String> generators, final Integer floor) {
final Map<String, Integer> newGenerators = new HashMap<>(this.gennys);
generators.forEach(g -> newGenerators.put(g, floor));
return this.withGennys(newGenerators);
}
private boolean floorsBelowEmpty(final Integer floor) {
for (int f = floor; f > BOTTOM; f--) {
if (this.chipsPerFloor.get(f - 1) != null) {
return false;
}
if (this.gennysPerFloor.get(f - 1) != null) {
return false;
}
}
return true;
}
@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final State other = (State) obj;
return Objects.equals(elevator, other.elevator)
&& Objects.equals(chips, other.chips)
&& Objects.equals(gennys, other.gennys);
}
@Override
public int hashCode() {
return Objects.hash(chips, elevator, gennys);
}
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
builder.append("State [elevator=").append(elevator)
.append(", chips=").append(chips)
.append(", gennys=").append(gennys)
.append(", isSafe=").append(isSafe()).append("]");
return builder.toString();
}
}
record Step(int numberOfSteps, State state) {;
public static Step of(final int numberOfSteps, final State state) {
return new Step(numberOfSteps, state);
}
}
}