-
Notifications
You must be signed in to change notification settings - Fork 1
/
CodingameSponsoredContest.java
424 lines (342 loc) · 12.2 KB
/
CodingameSponsoredContest.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
import java.util.*;
import java.util.stream.Collectors;
class CodingameSponsoredContest {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
final Init init = readInit(in);
final List<GameTurn> gameTurns = new ArrayList<>();
final Cartography cartography = new Cartography();
int turn = 1;
// game loop
while (true) {
final GameTurn gameTurn = readGameInput(init, turn++, in);
cartography.addSurrounding(gameTurn);
gameTurns.add(gameTurn);
// every turn print A, B, C, D or E
// this feels like movement on a map
// maybe try left wall algorithm
// maybe add mapping algorithm to draw map for me
// check numbers, completely ignored so far :)
if (gameTurn.getC().equals("_")) {
gameTurn.setOutput('D');
} else if (gameTurn.getA().equals("_")) {
gameTurn.setOutput('C');
} else if (gameTurn.getB().equals("_")) {
gameTurn.setOutput('A');
} else if (gameTurn.getD().equals("_")) {
gameTurn.setOutput('E');
} else {
gameTurn.setOutput('B'); // no clue what this does
}
//printHistory(gameTurns);
cartography.print();
System.out.println(gameTurn.getOutput());
}
}
private static void printHistory(List<GameTurn> gameTurns) {
StringBuilder result = new StringBuilder();
for (GameTurn gameTurn : gameTurns) {
result.append(gameTurn.toCompactString()).append('\n');
}
System.err.print(result);
}
private static GameTurn readGameInput(Init init, int turn, Scanner in) {
final GameTurn gameTurn = new GameTurn(turn, in.nextLine(), in.nextLine(), in.nextLine(), in.nextLine());
for (int i = 0; i < init.C; i++) {
gameTurn.addTuple(readTuple(in));
}
in.nextLine();
System.err.println("Input: " + gameTurn);
return gameTurn;
}
private static Tuple readTuple(Scanner in) {
return new Tuple(in.nextInt(), in.nextInt());
}
private static Init readInit(Scanner in) {
final Init init = new Init(in.nextInt(), in.nextInt(), in.nextInt());
if (in.hasNextLine()) {
in.nextLine();
}
System.err.println("Init: " + init);
return init;
}
private static class Init {
private final int A;
private final int B;
private final int C;
private Init(int a, int b, int c) {
A = a;
B = b;
C = c;
}
@Override
public String toString() {
return String.format("Init{A=%d, B=%d, C=%d}", A, B, C);
}
public int getA() {
return A;
}
public int getB() {
return B;
}
public int getC() {
return C;
}
}
private static class GameTurn {
private final int turn;
private final String A;
private final String B;
private final String C;
private final String D;
private final List<Tuple> tuples;
private char output;
private GameTurn(int turn, String a, String b, String c, String d) {
this.turn = turn;
this.A = a;
this.B = b;
this.C = c;
this.D = d;
this.tuples = new ArrayList<>();
}
private void addTuple(Tuple tuple) {
tuples.add(tuple);
}
@Override
public String toString() {
return String.format("GameTurn{turn=%d, A='%s', B='%s', C='%s', D='%s', tuples=%s, output=%s}", turn, A, B, C, D, tuples, output);
}
public String toCompactString() {
return String.format("%02d %s%s%s%s -> %s (%s)", turn, A, B, C, D, output, getTuplesAsCompactString());
}
private String getTuplesAsCompactString() {
return tuples.stream().map(Tuple::toString).collect(Collectors.joining(","));
}
public String getA() {
return A;
}
public String getB() {
return B;
}
public String getC() {
return C;
}
public String getD() {
return D;
}
public List<Tuple> getTuples() {
return tuples;
}
public char getOutput() {
return output;
}
public void setOutput(char output) {
this.output = output;
}
public String getDirectionsAsString() {
return String.format("%s%s%s%s", A, B, C, D);
}
}
private static class Tuple {
private final Integer X;
private final Integer Y;
private Tuple(Integer x, Integer y) {
X = x;
Y = y;
}
@Override
public String toString() {
return String.format("(%d/%d)", X, Y);
}
public boolean isNeighbor(Tuple other) {
return (this.X.equals(other.X) && Math.abs(this.Y - other.Y) == 1) ||
(this.Y.equals(other.Y) && Math.abs(this.X - other.X) == 1);
}
public Tuple getNeighbor(Direction direction) {
return add(direction.getDirectionalOffset());
}
public Tuple add(Tuple other) {
return new Tuple(this.X + other.X, this.Y + other.Y);
}
}
private static class Cartography {
private BoundingBox boundingBox;
private Tuple position;
private Map<Tuple, Field> map;
public Cartography() {
map = new HashMap<>();
position = new Tuple(0, 0);
boundingBox = new BoundingBox(position);
final Field startingField = new Field(position, FieldContent.EMPTY);
addField(startingField);
startingField.visit();
}
public void addSurrounding(GameTurn gameTurn) {
final CartographyInputDto inputDto = new CartographyInputDto(gameTurn.getDirectionsAsString());
for (Direction direction : inputDto.getMappedDirections()) {
final Tuple neighborPosition = position.getNeighbor(direction);
final FieldContent content = inputDto.getFieldContent(direction);
final Field field = new Field(neighborPosition, content);
verifyOrAddField(field);
}
}
private void verifyOrAddField(Field field) {
if (containsField(field)) {
if (map.get(field.getPosition()) != field) {
throw new InputMismatchException();
}
} else {
addField(field);
}
}
public void visit(Tuple newPosition) {
if (!position.isNeighbor(newPosition)) {
throw new IllegalArgumentException(String.format("Position %s can not be reached in one step", newPosition));
}
position = newPosition;
map.get(newPosition).visit();
}
public void addField(Field field) {
map.put(field.getPosition(), field);
boundingBox.extend(field.getPosition());
}
public boolean containsField(Field field) {
return map.containsKey(field.getPosition());
}
public void print() {
StringBuilder output = new StringBuilder();
// iterate bounding box and get from map and print ' ', _, #
}
}
private static class BoundingBox {
private Tuple topLeft;
private Tuple bottomRight;
public BoundingBox(Tuple startingPosition) {
this.topLeft = this.bottomRight = startingPosition;
}
public boolean isInside(Tuple position) {
return topLeft.X <= position.X &&
topLeft.Y >= position.Y &&
bottomRight.X >= position.X &&
bottomRight.Y <= position.Y;
}
public void extend(Tuple position) {
if (topLeft.X > position.X) {
topLeft = new Tuple(position.X, topLeft.Y);
}
if (topLeft.Y < position.Y) {
topLeft = new Tuple(topLeft.X, position.Y);
}
if (bottomRight.X < position.X) {
bottomRight = new Tuple(position.X, bottomRight.Y);
}
if (bottomRight.Y > position.Y) {
bottomRight = new Tuple(bottomRight.X, position.Y);
}
}
}
private static class Field {
private final Tuple position;
private final FieldContent content;
private FieldState state;
public Field(Tuple position, FieldContent content) {
this.position = position;
this.content = content;
this.state = FieldState.UNVISITED;
}
public boolean canVisit() {
return content.canVisit();
}
public boolean isFirstVisit() {
return content.canVisit() && state == FieldState.UNVISITED;
}
public void visit() {
state = FieldState.VISITED;
}
public Tuple getPosition() {
return position;
}
public FieldContent getContent() {
return content;
}
public FieldState getState() {
return state;
}
}
private enum FieldContent {
EMPTY('_', true),
WALL('#', false);
private final char character;
private final boolean canVisit;
private FieldContent(char character, boolean canVisit) {
this.character = character;
this.canVisit = canVisit;
}
@Override
public String toString() {
return String.valueOf(character);
}
public char getCharacter() {
return character;
}
public boolean canVisit() {
return canVisit;
}
public static FieldContent getForCharacter(char character) {
return Arrays.stream(FieldContent.values())
.filter(content -> content.character == character)
.findFirst()
.orElseThrow(FieldContentNotFoundException::new);
}
}
private static class FieldContentNotFoundException extends IllegalArgumentException {
}
private enum FieldState {
UNVISITED,
VISITED;
}
private enum Direction {
NORTH(0, new Tuple(+1, 0)), // todo fix this
EAST(1, new Tuple(+1, 0)),
SOUTH(2, new Tuple(+1, 0)),
WEST(3, new Tuple(+1, 0));
private final Integer inputOffset;
private final Tuple directionalOffset;
Direction(Integer inputOffset, Tuple directionalOffset) {
this.inputOffset = inputOffset;
this.directionalOffset = directionalOffset;
}
public Tuple getDirectionalOffset() {
return directionalOffset;
}
public Integer getInputOffset() {
return inputOffset;
}
}
private static class CartographyInputDto {
private final String inputString;
private final Map<Direction, FieldContent> input;
private CartographyInputDto(String inputString) {
this.inputString = inputString;
this.input = parse(inputString);
}
private Map<Direction, FieldContent> parse(String inputString) {
final Map<Direction, FieldContent> result = new HashMap<>();
for (Direction direction : Direction.values()) {
final char inputCharacter = inputString.charAt(direction.getInputOffset());
final FieldContent content = FieldContent.getForCharacter(inputCharacter);
result.put(direction, content);
}
return result;
}
public Map<Direction, FieldContent> getInput() {
return input;
}
public Set<Direction> getMappedDirections() {
return input.keySet();
}
public FieldContent getFieldContent(Direction direction) {
return input.get(direction);
}
}
}