-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd23.java
329 lines (277 loc) · 21.2 KB
/
d23.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
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.HashMap;
import java.util.LinkedList;
class d23 {
int[][] costs;
int[][][] paths;
int[] init;
String target;
void initParams(boolean part2, String f) throws Exception {
BufferedReader br = new BufferedReader(new FileReader(new File(f)));
br.readLine();
br.readLine();
String s1 = br.readLine();
String s2 = br.readLine();
br.close();
int d1 = (int) (s1.charAt(3)) - 64;
int d2 = (int) (s1.charAt(5)) - 64;
int d3 = (int) (s1.charAt(7)) - 64;
int d4 = (int) (s1.charAt(9)) - 64;
int d5 = (int) (s2.charAt(3)) - 64;
int d6 = (int) (s2.charAt(5)) - 64;
int d7 = (int) (s2.charAt(7)) - 64;
int d8 = (int) (s2.charAt(9)) - 64;
if (!part2) {
// The map of nodes looks like this...
// 0 1 * 2 * 3 * 4 * 5 6
// 7 9 11 13
// 8 10 12 14
init = new int[] {0, 0, 0, 0, 0, 0, 0, d1, d5, d2, d6, d3, d7, d4, d8};
target = "000000011223344";
costs = new int[][] {
{-1, -1, -1, -1, -1, -1, -1, 3, 4, 5, 6, 7, 8, 9, 10},
{-1, -1, -1, -1, -1, -1, -1, 2, 3, 4, 5, 6, 7, 8, 9},
{-1, -1, -1, -1, -1, -1, -1, 2, 3, 2, 3, 4, 5, 6, 7},
{-1, -1, -1, -1, -1, -1, -1, 4, 5, 2, 3, 2, 3, 4, 5},
{-1, -1, -1, -1, -1, -1, -1, 6, 7, 4, 5, 2, 3, 2, 3},
{-1, -1, -1, -1, -1, -1, -1, 8, 9, 6, 7, 4, 5, 2, 3},
{-1, -1, -1, -1, -1, -1, -1, 9, 10, 7, 8, 5, 6, 3, 4},
{ 3, 2, 2, 4, 6, 8, 9, -1, -1, 4, 5, 6, 7, 8, 9},
{ 4, 3, 3, 5, 7, 9, 10, -1, -1, 5, 6, 7, 8, 9, 10},
{ 5, 4, 2, 2, 4, 6, 7, 4, 5, -1, -1, 4, 5, 6, 7},
{ 6, 5, 3, 3, 5, 7, 8, 5, 6, -1, -1, 5, 6, 7, 8},
{ 7, 6, 4, 2, 2, 4, 5, 6, 7, 4, 5, -1, -1, 4, 5},
{ 8, 7, 5, 3, 3, 5, 6, 7, 8, 5, 6, -1, -1, 5, 6},
{ 9, 8, 6, 4, 2, 2, 3, 8, 9, 6, 7, 4, 5, -1, -1},
{10, 9, 7, 5, 3, 3, 4, 9, 10, 7, 8, 5, 6, -1, -1}
};
paths = new int[][][] {
{{-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {1}, {1,7}, {1,2}, {1,2,9}, {1,2,3}, {1,2,3,11}, {1,2,3,4}, {1,2,3,4,13}},
{{-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {7}, {2}, {2,9}, {2,3}, {2,3,11}, {2,3,4}, {2,3,4,13}},
{{-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {7}, {-1}, {9}, {3}, {3,11}, {3,4}, {3,4,13}},
{{-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {2}, {2,7}, {-1}, {9}, {-1}, {11}, {4}, {4,13}},
{{-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {2,3}, {2,3,7}, {3}, {3,9}, {-1}, {11}, {-1}, {13}},
{{-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {2,3,4}, {2,3,4,7}, {3,4}, {3,4,9}, {4}, {4,11}, {-1}, {13}},
{{-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {2,3,4,5}, {2,3,4,5,7}, {3,4,5}, {3,4,5,9}, {4,5}, {4,5,11}, {5}, {5,13}},
{ {1}, {-1}, {-1}, {2}, {2,3}, {2,3,4}, {2,3,4,5}, {-1}, {-1}, {2}, {2,9}, {2,3}, {2,3,11}, {2,3,4}, {2,3,4,13}},
{ {1,7}, {7}, {7}, {7,2}, {7,2,3}, {7,2,3,4}, {7,2,3,4, 5}, {-1}, {-1}, {7,2}, {7,2,9}, {7,2,3}, {7,2,3,11}, {2,3,4,7}, {2,3,4,7, 13}},
{ {1,2}, {2}, {-1}, {-1}, {3}, {3,4}, {3,4,5}, {2}, {2,7}, {-1}, {-1}, {3}, {3,11}, {3,4}, {3,4,13}},
{ {1,2,9}, {2,9}, {9}, {9}, {3,9}, {9,3,4}, {9,3,4,5}, {9,2}, {9,2,7}, {-1}, {-1}, {9,3}, {3,9,11}, {3,4,9}, {3,4,9, 13}},
{ {1,2,3}, {2,3}, {3}, {-1}, {-1}, {4}, {4,5}, {2,3}, {2,3,7}, {3}, {3,9}, {-1}, {-1}, {4}, {4,13}},
{ {1,2,3,11}, {2,3,11}, {3,11}, {11}, {11}, {11,4}, {11,4,5}, {11,3,2}, {11,3,2,7}, {11,3}, {11,3,9}, {-1}, {-1}, {4,11}, {4,11, 13}},
{ {1,2,3,4}, {2,3,4}, {3,4}, {4}, {-1}, {-1}, {5}, {2,3,4}, {2,3,4,7}, {3,4}, {3,4,9}, {4}, {4,11}, {-1}, {-1}},
{ {1,2,3,4,13}, {2,3,4,13}, {3,4,13}, {4,13}, {13}, {13}, {13,5}, {13,4,3,2}, {13,4,3,2,7}, {13,4,3}, {13,4,3,9}, {13,4}, {4,13,11}, {-1}, {-1}}
};
} else {
// The map of nodes now looks like this...
// 0 1 * 2 * 3 * 4 * 5 6
// 7 9 11 13
// 8 10 12 14
// 15 17 19 21
// 16 18 20 22
init = new int[] {0, 0, 0, 0, 0, 0, 0, d1, 4, d2, 3, d3, 2, d4, 1, 4, d5, 2, d6, 1, d7, 3, d8};
target = "00000001122334411223344";
costs = new int[][] {
{-1, -1, -1, -1, -1, -1, -1, 3, 4, 5, 6, 7, 8, 9, 10, 5, 6, 7, 8, 9, 10, 11, 12},
{-1, -1, -1, -1, -1, -1, -1, 2, 3, 4, 5, 6, 7, 8, 9, 4, 5, 6, 7, 8, 9, 10, 11},
{-1, -1, -1, -1, -1, -1, -1, 2, 3, 2, 3, 4, 5, 6, 7, 4, 5, 4, 5, 6, 7, 8, 9},
{-1, -1, -1, -1, -1, -1, -1, 4, 5, 2, 3, 2, 3, 4, 5, 6, 7, 4, 5, 4, 5, 6, 7},
{-1, -1, -1, -1, -1, -1, -1, 6, 7, 4, 5, 2, 3, 2, 3, 8, 9, 6, 7, 4, 5, 4, 5},
{-1, -1, -1, -1, -1, -1, -1, 8, 9, 6, 7, 4, 5, 2, 3, 10, 11, 8, 9, 6, 7, 4, 5},
{-1, -1, -1, -1, -1, -1, -1, 9, 10, 7, 8, 5, 6, 3, 4, 11, 12, 9, 10, 7, 8, 5, 6},
{ 3, 2, 2, 4, 6, 8, 9, -1, -1, 4, 5, 6, 7, 8, 9, -1, -1, 6, 7, 8, 9, 10, 11},
{ 4, 3, 3, 5, 7, 9, 10, -1, -1, 5, 6, 7, 8, 9, 10, -1, -1, 7, 8, 9, 10, 11, 12},
{ 5, 4, 2, 2, 4, 6, 7, 4, 5, -1, -1, 4, 5, 6, 7, 6, 7, -1, -1, 6, 7, 8, 9},
{ 6, 5, 3, 3, 5, 7, 8, 5, 6, -1, -1, 5, 6, 7, 8, 7, 8, -1, -1, 7, 8, 9, 10},
{ 7, 6, 4, 2, 2, 4, 5, 6, 7, 4, 5, -1, -1, 4, 5, 8, 9, 6, 7, -1, -1, 6, 7},
{ 8, 7, 5, 3, 3, 5, 6, 7, 8, 5, 6, -1, -1, 5, 6, 9, 10, 7, 8, -1, -1, 7, 8},
{ 9, 8, 6, 4, 2, 2, 3, 8, 9, 6, 7, 4, 5, -1, -1, 10, 11, 8, 9, 6, 7, -1, -1},
{10, 9, 7, 5, 3, 3, 4, 9, 10, 7, 8, 5, 6, -1, -1, 11, 12, 9, 10, 7, 8, -1, -1},
{ 5, 4, 4, 6, 8, 10, 11, -1, -1, 6, 7, 8, 9, 10, 11, -1, -1, 8, 9, 10, 11, 12, 13},
{ 6, 5, 5, 7, 9, 11, 12, -1, -1, 7, 8, 9, 10, 11, 12, -1, -1, 9, 10, 11, 12, 13, 14},
{ 7, 6, 4, 4, 6, 8, 9, 6, 7, -1, -1, 6, 7, 8, 9, 8, 9, -1, -1, 8, 9, 10, 11},
{ 8, 7, 5, 5, 7, 9, 10, 7, 8, -1, -1, 7, 8, 9, 10, 9, 10, -1, -1, 9, 10, 11, 12},
{ 9, 8, 6, 4, 4, 6, 7, 8, 9, 6, 7, -1, -1, 6, 7, 10, 11, 8, 9, -1, -1, 8, 9},
{10, 9, 7, 5, 5, 7, 8, 9, 10, 7, 8, -1, -1, 7, 8, 11, 12, 9, 10, -1, -1, 9, 10},
{11, 10, 8, 6, 4, 4, 5, 10, 11, 8, 9, 6, 7, -1, -1, 12, 13, 10, 11, 8, 9, -1, -1},
{12, 11, 9, 7, 5, 5, 6, 11, 12, 9, 10, 7, 8, -1, -1, 13, 14, 11, 12, 9, 10, -1, -1}
};
paths = new int[][][] {
{{-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {1}, {1,7}, {1,2}, {1,2,9}, {1,2,3}, {1,2,3,11}, {1,2,3,4}, {1,2,3,4,13}, {1, 7, 8}, {1,7,8,15}, {1,2,9,10}, {1,2,9,10,17}, {1,2,3,11,12}, {1,2,3,11,12,19}, {1,2,3,4,13,14}, {1,2,3,4,13,14,21}},
{{-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {7}, {2}, {2,9}, {2,3}, {2,3,11}, {2,3,4}, {2,3,4,13}, {7,8}, {7,8,15}, {2,9,10},{2,9,10,17}, {2,3,11,12}, {2,3,11,12,9}, {2,3,4,13,14},{2,3,4,13,14,21}},
{{-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {7}, {-1}, {9}, {3}, {3,11}, {3,4}, {3,4,13}, {7,8}, {7,8,15}, {9,10}, {9,10,17}, {3,11,12}, {3,11,12,19}, {3,4,13,14}, {3,4,13,14,21}},
{{-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {2}, {2,7}, {-1}, {9}, {-1}, {11}, {4}, {4,13}, {2,7,8}, {2,7,8,15}, {9,10}, {9,10,17}, {11,12}, {11,12,19}, {4,13,14}, {4,13,14,21}},
{{-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {3,2}, {3,2,7}, {3}, {3,9}, {-1}, {11}, {-1}, {13}, {3,2,7,8}, {3,2,7,8,15}, {3,9,10}, {3,9,10,17}, {11,12}, {11,12,19}, {13,14}, {13,14,21}},
{{-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {2,3,4}, {2,3,4,7}, {3,4}, {3,4,9}, {4}, {4,11}, {-1}, {13}, {4,3,2,7,8}, {4,3,2,7,8,15}, {4,3,9,10}, {4,3,9,10,17}, {4,11,12}, {4,11,12,19}, {13,14}, {13,14,21}},
{{-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {2,3,4,5}, {2,3,4,5,7}, {3,4,5}, {3,4,5,9}, {4,5}, {4,5,11}, {5}, {5,13}, {5,4,3,2,7,8}, {5,4,3,2,7,8,15}, {5,4,3,9,10}, {5,4,3,9,10,17}, {5,4,11,12}, {5,4,11,12,19}, {5,13,14}, {5,13,14,21}},
{{1}, {-1}, {-1}, {2}, {2,3}, {2,3,4}, {2,3,4,5}, {-1}, {-1}, {2}, {2,9}, {2,3}, {2,3,11}, {2,3,4}, {2,3,4,13}, {-1}, {-1}, {2,9,10}, {2,9,10,17}, {2,3,11,12}, {2,3,11,12,19}, {2,3,4,13,14} ,{2,3,4,13,14,21}},
{{1,7}, {7}, {7}, {7,2}, {7,2,3}, {7,2,3,4}, {7,2,3,4, 5}, {-1}, {-1}, {7,2}, {7,2,9}, {7,2,3}, {7,2,3,11}, {2,3,4,7}, {2,3,4,7, 13}, {-1}, {-1}, {7,2,9,10}, {7,2,9,10,17}, {7,2,3,11,12}, {7,2,3,11,12,19}, {7,2,3,4,13,14}, {7,2,3,4,13,14,21}},
{{1,2}, {2}, {-1}, {-1}, {3}, {3,4}, {3,4,5}, {2}, {2,7}, {-1}, {-1}, {3}, {3,11}, {3,4}, {3,4,13}, {2,7,8}, {2,7,8,15}, {-1}, {-1}, {3,11,12}, {3,11,12,19}, {3,4,13,14}, {3,4,13,14,21}},
{{1,2,9}, {2,9}, {9}, {9}, {3,9}, {9,3,4}, {9,3,4,5}, {9,2}, {9,2,7}, {-1}, {-1}, {9,3}, {3,9,11}, {3,4,9}, {3,4,9, 13}, {9,2,7,8}, {9,2,7,8,15}, {-1}, {-1}, {9,3,11,12}, {9,3,11,12,19}, {9,3,4,13,14}, {9,3,4,13,14,21}},
{{1,2,3}, {2,3}, {3}, {-1}, {-1}, {4}, {4,5}, {2,3}, {2,3,7}, {3}, {3,9}, {-1}, {-1}, {4}, {4,13}, {3,2,7,8}, {3,2,7,8,15}, {3,9,10}, {3,9,10,17}, {-1}, {-1}, {4,13,14}, {4,13,14,21}},
{{1,2,3,11}, {2,3,11}, {3,11}, {11}, {11}, {11,4}, {11,4,5}, {11,3,2}, {11,3,2,7}, {11,3}, {11,3,9}, {-1}, {-1}, {4,11}, {4,11, 13}, {11,3,2,7,8}, {11,3,2,7,8,15}, {11,3,9,10}, {11,3,9,10,17}, {-1}, {-1}, {11,4,13,14}, {11,4,13,14,21}},
{{1,2,3,4}, {2,3,4}, {3,4}, {4}, {-1}, {-1}, {5}, {2,3,4}, {2,3,4,7}, {3,4}, {3,4,9}, {4}, {4,11}, {-1}, {-1}, {4,3,2,7,8},{4,3,2,7,8,15},{4,3,9,10},{4,3,9,10,17},{4,11,12},{4,11,12,19},{-1},{-1}},
{{1,2,3,4,13}, {2,3,4,13}, {3,4,13}, {4,13}, {13}, {13}, {13,5}, {13,4,3,2}, {13,4,3,2,7}, {13,4,3}, {13,4,3,9}, {13,4}, {4,13,11}, {-1}, {-1}, {13,4,3,2,7,8}, {13,4,3,2,7,8,15},{13,4,3,9,10}, {13,4,3,9,10,17},{13,4,11,12},{13,4,11,12,19},{-1},{-1}},
{{8,7,1}, {8,7}, {8,7}, {8,7,2}, {8,7,2,3}, {8,7,2,3,4}, {8,7,2,3,4,5}, {-1}, {-1}, {8,7,2}, {8,7,2,9}, {8,7,2,3}, {8,7,2,3,11}, {8,7,2,3,4}, {8,7,2,3,4,13},{-1}, {-1}, {8,7,2,9,10}, {8,7,2,9,10,17}, {8,7,2,3,11,12}, {8,7,2,3,11,12,19}, {8,7,2,3,4,13,14}, {8,7,2,3,4,13,14,21}},
{{15,8,7,1}, {15,8,7}, {15,8,7}, {15,8,7,2}, {15,8,7,2,3}, {15,8,7,2,3,4}, {15,8,7,2,3,4,5}, {-1}, {-1}, {15,8,7,2}, {15,8,7,2,9}, {15,8,7,2,3}, {15,8,7,2,3,11}, {15,8,7,2,3,4}, {15,8,7,2,3,4,13}, {-1}, {-1}, {15,8,7,2,9,10}, {15,8,7,2,9,10,17}, {15,8,7,2,3,11,12}, {15,8,7,2,3,11,12,19}, {15,8,7,2,3,4,13,14}, {15,8,7,2,3,4,13,14,21}},
{{10,9,2,1}, {10,9,2}, {10,9}, {10,9}, {10,9,3}, {10,9,3,4}, {10,9,3,4,5}, {10,9,2}, {10,9,2,7}, {-1}, {-1}, {10,9,3}, {10,9,3,11}, {10,9,3,4}, {10,9,3,4,13}, {10,9,2,7,8}, {10,9,2,7,8,15}, {-1}, {-1}, {10,9,3,11,12}, {10,9,3,11,12,19}, {10,9,3,4,13,14}, {10,9,3,4,13,14,21}},
{{17,10,9,2,1}, {17,10,9,2}, {17,10,9}, {17,10,9}, {17,10,9,3}, {17,10,9,3,4}, {17,10,9,3,4,5}, {17,10,9,2}, {17,10,9,2,7}, {-1}, {-1}, {17,10,9,3}, {17,10,9,3,11}, {17,10,9,3,4}, {17,10,9,3,4,13}, {17,10,9,2,7,8}, {17,10,9,2,7,8,15}, {-1}, {-1}, {17,10,9,3,11,12}, {17,10,9,3,11,12,19}, {17,10,9,3,4,13,14}, {17,10,9,3,4,13,14,21}},
{{12,11,3,2,1}, {12,11,3,2}, {12,11,3}, {12,11}, {12,11}, {12,11,4}, {12,11,4,5}, {12,11,3,2}, {12,11,3,2,7}, {12,11,3}, {12,11,3,9}, {-1}, {-1}, {12,11,4}, {12,11,4,13}, {12,11,3,2,7,8}, {12,11,3,2,7,8,15}, {12,11,3,9,10}, {12,11,3,9,10,17}, {-1}, {-1}, {12,11,4,13,14}, {12,11,4,13,14,21}},
{{19,12,11,3,2,1}, {19,12,11,3,2}, {19,12,11,3}, {19,12,11}, {19,12,11}, {19,12,11,4}, {19,12,11,4,5}, {19,12,11,3,2}, {19,12,11,3,2,7}, {19,12,11,3}, {19,12,11,3,9}, {-1}, {-1}, {19,12,11,4}, {19,12,11,4,13}, {19,12,11,3,2,7,8}, {19,12,11,3,2,7,8,15}, {19,12,11,3,9,10}, {19,12,11,3,9,10,17}, {-1}, {-1}, {19,12,11,4,13,14}, {19,12,11,4,13,14,21}},
{{14,13,4,3,2,1}, {14,13,4,3,2}, {14,13,4,3}, {14,13,4}, {14,13}, {14,13}, {14,13,5}, {14,13,4,3,2}, {14,13,4,3,2,7}, {14,13,4,3}, {14,13,4,3,9}, {14,13,4}, {14,13,4,11}, {-1}, {-1}, {14,13,4,3,2,7,8}, {14,13,4,3,2,7,8,15}, {14,13,4,3,9,10}, {14,13,4,3,9,10,17}, {14,13,4,11,12}, {14,13,4,11,12,19},{-1},{-1}},
{{21,14,13,4,3,2,1}, {21,14,13,4,3,2}, {21,14,13,4,3}, {21,14,13,4}, {21,14,13}, {21,14,13}, {21,14,13,5}, {21,14,13,4,3,2}, {21,14,13,4,3,2,7}, {21,14,13,4,3}, {21,14,13,4,3,9}, {21,14,13,4}, {21,14,13,4,11}, {-1}, {-1}, {21,14,13,4,3,2,7,8}, {21,14,13,4,3,2,7,8,15}, {21,14,13,4,3,9,10}, {21,14,13,4,3,9,10,17}, {21,14,13,4,11,12}, {21,14,13,4,11,12,19},{-1},{-1}}};
}
}
String pack(int[] state, boolean part2) {
return String.valueOf(state[0]) + state[1] + state[2] + state[3] + state[4] + state[5] +
state[6] + state[7] + state[8] + state[9] + state[10] + state[11] +
state[12] + state[13] + state[14] + ((part2) ?
"" + state[15] + state[16] + state[17] + state[18] + state[19] +
state[20] + state[21] + state[22] : "");
}
int go_home(int[] state, int pod, int p4, int p3, int p2, int p1, boolean part2) {
// If you're in the hallway, then there's either one, or zero destinations for you.
if (part2) {
if (state[p4] == 0) return p4;
else if ((state[p3] == 0) && (state[p4] == pod)) return p3;
else if ((state[p2] == 0) && (state[p3] == pod) && (state[p4] == pod)) return p2;
else if ((state[p1] == 0) && (state[p2] == pod) && (state[p3] == pod) && (state[p4] == pod)) return p1;
else return -1;
}
if (state[p2] == 0) return p2;
else if ((state[p2] == pod) && (state[p1] == 0)) return p1;
else return -1;
}
boolean worth_moving(int[] state, int pod, int room,
int p4, int p3, int p2, int p1, boolean part2) {
// If anyone is above you, you can't move.
if ((room == p2) && (state[p1] !=0)) return false;
if (part2) {
if ((room == p4) && (state[p3] !=0)) return false;
if ((room == p3) && (state[p2] !=0)) return false;
if ((room == p2) && (state[p1] !=0)) return false;
}
// If you're not in the right slot, you want to move.
boolean right_slot = (room == p1) || (room == p2) ||
(part2 ? ((room == p3) || (room == p4)) : false);
if (!right_slot) return true;
// If you're in the right slot, but a different pod is
// below you, you want to move.
if (part2) {
if ((room == p3) && (state[p4] != pod)) return true;
if ((room == p2) && ((state[p3] != pod) || (state[p4] != pod))) return true;
if ((room == p1) && ((state[p2] != pod) || (state[p3] != pod) || (state[p4] != pod))) return true;
} else {
if ((room == p1) && (state[p2] != pod)) return true;
}
// Otherwise, don't move.
return false;
}
int[] unpack(String state) {
int[] st = new int[state.length()];
for (int i=0; i<state.length(); i++) {
st[i] = Integer.parseInt(""+state.charAt(i));
}
return st;
}
int solve(boolean part2) {
int[] pcost = new int[] {0, 1, 10, 100, 1000};
HashMap<String, Integer> history = new HashMap<String, Integer>();
history.put(pack(init, part2), 0);
LinkedList<String> stateq = new LinkedList<String>();
LinkedList<Integer> costq = new LinkedList<Integer>();
stateq.add(pack(init, part2));
costq.add(0);
int best = Integer.MAX_VALUE;
while (stateq.size() > 0) {
int[] state0 = unpack(stateq.pop());
int cost0 = costq.pop();
if (cost0 > best) continue;
for (int room = 0; room < state0.length; room++) {
if (state0[room] == 0) continue; // Continue if there's a pod here.
int pod = state0[room];
// Default: If you're not in the hallway, and you want to move sooner
// or later, then try all the hallway positions.
int dest1 = 0;
int dest2 = 6;
// If you're in the hallway... then the only dest you can move to is the
// top-most empty slot in your "home" - if any filled slots are also
// filled by your type.
if (room <= 6) {
if (pod == 1) dest1 = go_home(state0, pod, 16, 15, 8, 7, part2);
else if (pod == 2) dest1 = go_home(state0, pod, 18, 17, 10, 9, part2);
else if (pod == 3) dest1 = go_home(state0, pod, 20, 19, 12, 11, part2);
else if (pod == 4) dest1 = go_home(state0, pod, 22, 21, 14, 13, part2);
dest2 = dest1;
// If you're not in the hallway, then don't bother moving if
// you're already in a good place (ie, you're in the right slow,
// and everything below you is.
} else {
boolean wm = false;
if (pod == 1) wm = worth_moving(state0, pod, room, 16, 15, 8, 7, part2);
else if (pod == 2) wm = worth_moving(state0, pod, room, 18, 17, 10, 9, part2);
else if (pod == 3) wm = worth_moving(state0, pod, room, 20, 19, 12, 11, part2);
else if (pod == 4) wm = worth_moving(state0, pod, room, 22, 21, 14, 13, part2);
if (!wm) {
dest1 = -1;
dest2 = -1;
}
}
if (dest1 == -1) continue;
for (int dest = dest1; dest <= dest2; dest++) {
if (state0[dest] != 0) continue; // Where dest is empty
boolean clear = true;
for (int via = 0; via < paths[room][dest].length; via++) {
if (paths[room][dest][via] == -1) {
clear = true;
break;
} else if (state0[paths[room][dest][via]] != 0) {
clear = false;
break;
}
}
if (!clear) continue; // Route is clear
// Make the move.
int[] state = new int[init.length];
for (int i=0; i<state.length; i++) state[i] = state0[i];
int cost = cost0;
state[dest] = pod;
state[room] = 0;
cost += pcost[pod] * costs[room][dest];
String pack = pack(state, part2);
if (pack.equals(target)) {
best = Math.min(cost, best);
continue;
}
if (history.containsKey(pack)) {
int prev_cost = history.get(pack);
if (prev_cost <= cost) continue;
}
history.put(pack, cost);
stateq.add(pack);
costq.add(cost);
}
}
}
return best;
}
public static void main(String[] args) throws Exception {
d23 W = new d23();
W.initParams(false, "../inputs/d23-test.txt");
if (W.solve(false) != 12521) System.out.println("Test 1 failed!");
long t = System.currentTimeMillis();
W.initParams(false, "../inputs/d23-input.txt");
System.out.print("Part 1: "+W.solve(false));
t = System.currentTimeMillis() - t;
System.out.println(" ("+t+" ms)");
W.initParams(true, "../inputs/d23-test.txt");
if (W.solve(true) != 44169) System.out.println("Test 2 failed!");
t = System.currentTimeMillis();
W.initParams(true, "../inputs/d23-input.txt");
System.out.print("Part 2: "+W.solve(true));
t = System.currentTimeMillis() - t;
System.out.println(" ("+t+" ms)");
}
}