[ create a new paste ] login | about

Link: http://codepad.org/iIqN1W2q    [ raw code | fork ]

D, pasted on Feb 1:
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
// Compiled with DMD 2.058 (also try 2.057)

import std.stdio, std.conv, std.datetime, std.string, std.array, std.exception;

enum : int { NONE =       -2,
             IMPOSSIBLE = -1,
             OPEN =        0,
             SOLVED =      1 }

// //////////////////////////////////////////////
int makePoint(in int x, in int y) pure nothrow {
    return ((x & 0xf) << 4) | (y & 0xf);
}

int pointX(in int point) pure nothrow {
    return (point >> 4) & 0xf;
}

int pointY(in int point) pure nothrow {
    return point & 0xf;
}

// //////////////////////////////////////////////
struct Done {
    private int done;
    private int[] cells;
    public int used;

    this(in int count) pure nothrow {
        this.cells = new int[count];
        this.cells[] = NONE;
    }

    public bool alreadyDone(in int i) const pure nothrow {
        return this.cells[i] != NONE;
    }

    public int nextCell() const pure nothrow {
        return this.done;
    }

    public int get(in int i) const pure nothrow {
        return this.cells[i];
    }

    private void adjustDone() pure nothrow {
        foreach (i; this.done .. this.cells.length) {
            if (this.cells[i] == NONE) {
                this.done = i;
                return;
            }
        }
        this.done = -1;
    }

    public void addDone(in int i, in int v) pure nothrow {
        this.cells[i] = v;
        this.used++;
        this.adjustDone();
    }

    public void removeDone(in int i) pure nothrow {
        this.cells[i] = NONE;
        this.used--;
        if (this.done < 0 || i < this.done)
            this.done = i;
    }
}

// //////////////////////////////////////////////
final class Node {
    public const int pos;
    public const int id;
    public int[] links;

    this(in int pos, in int id, int[] links) pure nothrow {
        this.pos = pos;
        this.id = id;
        this.links = links;
    }

    public void appendLink(in int id) pure nothrow {
        int[] nlinks = new int[this.links.length + 1];
        nlinks[0 .. this.links.length] = this.links[];
        nlinks[this.links.length] = id;
        this.links = nlinks;
    }

    public override string toString() const {
        string lstr = "[";
        foreach (i; 0 .. this.links.length) {
            if (i > 0)
                lstr ~= ",";
            lstr ~= text(i);
        }
        lstr ~= "]";
        return text("<id=", this.id, " ", this.pos, " ", lstr, ">");
    }
}

// //////////////////////////////////////////////
final class Hex {
    public const int size;
    public const int count;
    public Node[] nodesById;
    public Node[] nodesByPos;

    this(in int size) pure nothrow {
        this.size = size;
        this.count = 3 * size * (size - 1) + 1;
        this.nodesById = new Node[this.count];
        const int maxCode = 256;
        this.nodesByPos = new Node[maxCode];
        int id = 0;

        foreach (y; 0 .. size) {
            foreach (x; 0 .. size + y) {
                const int pos = makePoint(x, y);
                Node node = new Node(pos, id, null);
                this.nodesByPos[pos] = node;
                this.nodesById[id] = node;
                id++;
            }
        }

        foreach (y; 1 .. size) {
            foreach (x;  y .. size * 2 - 1) {
                const int ry = size + y - 1;
                const int pos = makePoint(x, ry);
                Node node = new Node(pos, id, null);
                this.nodesByPos[pos] = node;
                this.nodesById[id] = node;
                id++;
            }
        }
    }

    public void linkNodes() pure nothrow {
        __gshared immutable int[][] dirs = [[1, 0], [-1, 0], [0, 1], [0, -1], [1, 1], [-1, -1]];

        foreach (node; this.nodesById) {
            const int p = node.pos;
            foreach (d, dd; dirs) {
                const int nx = pointX(p) + dd[0];
                const int ny = pointY(p) + dd[1];
                const int ncode = makePoint(nx, ny);
                if (this.containsPos(ncode))
                    node.appendLink(this.nodesByPos[ncode].id);
            }
        }
    }

    public bool containsPos(in int code) const pure nothrow {
        return (code >= 0) && (code < this.nodesByPos.length) &&
               (this.nodesByPos[code] !is null);
    }

    public Node getByPos(in int pos) /*const*/ pure nothrow {
        return this.nodesByPos[pos];
    }

    public Node getById(in int id) /*const*/ pure nothrow {
        return this.nodesById[id];
    }
}

// //////////////////////////////////////////////
private int[] makeTiles() pure nothrow {
    return new int[8];
}

public int sumTiles(in int[] tiles) pure nothrow {
    int sum = 0;
    foreach (i; 0 .. 7)
        sum += tiles[i];
    return sum;
}

// //////////////////////////////////////////////
final class Pos {
    public Hex hex;
    public int[] tiles;
    public int sumTiles;
    public Done done;

    this(Hex hex, int[] tiles, Done done) pure nothrow {
        this.hex = hex;
        this.tiles = tiles;
        this.sumTiles = .sumTiles(tiles);
        this.done = done;
    }
}

// //////////////////////////////////////////////
private int findMoves(Pos pos, int[] moves) pure nothrow {
    int count, index;
    Hex hex = pos.hex;
    int[] tiles = pos.tiles;
    Done done = pos.done;
    const int cellId = done.nextCell();
    if (cellId < 0)
        return count;

    int[] cellsAround;
    int minPossible, maxPossible;
    foreach (i; 0 .. 8) {
        if (tiles[i] > 0) {
            bool valid = true;
            if (i < 7) {
                if (cellsAround == null) {
                    cellsAround = hex.getById(cellId).links;
                    maxPossible = cellsAround.length;
                    foreach (ca; 0 .. cellsAround.length) {
                        int j = cellsAround[ca];
                        if (done.alreadyDone(j)) {
                            int dj = done.get(j);
                            if (dj > 0 && dj < 7) {
                                minPossible++;
                            } else if (dj == 0) {
                                maxPossible = 0;
                                minPossible++;
                            }
                        }
                    }
                }

                valid = (minPossible <= i) && (i <= maxPossible);
            }

            if (valid) {
                moves[index] = cellId;
                moves[index + 1] = i;
                count++;
                index += 2;
            }
        }
    }
    return count;
}

private void playMove(Pos pos, in int cellId, in int value) pure nothrow {
    pos.tiles[value]--;
    if (value < 7)
        pos.sumTiles--;
    pos.done.addDone(cellId, value);
}

private void undoMove(Pos pos, in int cellId, in int value) pure nothrow {
    pos.tiles[value]++;
    if (value < 7)
        pos.sumTiles++;
    pos.done.removeDone(cellId);
}

private enum string SPACES = "                                                  ";

private void printPos(Pos pos) {
    Hex hex = pos.hex;
    const Done done = pos.done;
    const int size = hex.size;
    foreach (y; 0 .. size) {
        write(SPACES[0 .. size - y - 1]);
        foreach (x; 0 .. size + y) {
            const int pos2 = makePoint(x, y);
            const int id = hex.getByPos(pos2).id;
            if (done.alreadyDone(id) && (done.get(id) < 7))
                write(done.get(id));
            else
                write(".");
            write(" ");
        }
        writeln();
    }

    foreach (y; 1 .. size) {
        write(SPACES[0 .. y]);
        foreach (x; y .. size * 2 - 1) {
            const int ry = size + y - 1;
            const int pos2 = makePoint(x, ry);
            const int id = hex.getByPos(pos2).id;
            if (done.alreadyDone(id) && (done.get(id) < 7))
                write(done.get(id));
            else
                write(".");
            write(" ");
        }
        writeln();
    }
}

private int solved(Pos pos) {
    Hex hex = pos.hex;
    const Done done = pos.done;
    bool exact = true;
    foreach (i; 0 .. hex.count) {
        if (done.alreadyDone(i)) {
            int num = done.get(i);
            int min, max;
            if (num < 7) {
                int[] cellsAround = hex.getById(i).links; // slower if const
                foreach (nid; cellsAround) {
                    if (done.alreadyDone(nid)) {
                        if (done.get(nid) < 7) {
                            min++;
                            max++;
                        }
                    } else {
                        max++;
                    }
                }
                if (num < min || num > max) {
                    return IMPOSSIBLE;
                }
                if (num != min) {
                    exact = false;
                }
            }
        }
    }

    if (pos.sumTiles > 0 || !exact)
        return OPEN;
    printPos(pos);
    return SOLVED;
}

private int solveStep(Pos pos) {
    int[16] moves = void;
    immutable int count = findMoves(pos, moves);

    foreach (i; 0 .. count) {
        immutable int cellId = moves[2 * i];
        immutable int value = moves[2 * i + 1];
        playMove(pos, cellId, value);
        immutable int curStatus = solved(pos);
        int ret = OPEN;
        if (curStatus != OPEN) {
            ret = curStatus;
        } else if (solveStep(pos) == SOLVED) {
            ret = SOLVED;
        }
        undoMove(pos, cellId, value);
        if (ret == SOLVED)
            return ret;
    }

    return IMPOSSIBLE;
}

private void checkValid(Pos pos) {
    Hex hex = pos.hex;
    int[] tiles = pos.tiles;
    const Done done = pos.done;
    int tot = done.used;
    // Fill missing entries in tiles
    foreach (i; 0 .. 8) {
        if (tiles[i] > 0)
            tot += tiles[i];
        else
            tiles[i] = 0;
    }
    // Check total
    if (tot != hex.count) {
        throw new Exception(text("Invalid input. Expected ",
                                 hex.count, " tiles, got ", tot, "."));
    }
}

private int solve(Pos pos) {
    checkValid(pos);
    return solveStep(pos);
}

private Pos readFile(in string file) {
    string[] lines;
    auto input = File(file);
    try {
        string line;
        while (!(line = input.readln().stripRight()).empty) {
            lines ~= line;
        }
    } finally {
        input.close();
    }

    const int size = to!int(lines[0]);
    Hex hex = new Hex(size);
    int linei = 1;
    int[] tiles = makeTiles();
    Done done = Done(hex.count);
    foreach (y; 0 .. size) {
        const string line = lines[linei][size - y - 1 .. $];
        int p = 0;
        foreach (x; 0 .. size + y) {
            string tile = line[p .. p + 2];
            p += 2;
            int inctile = 0;
            if (tile[1] == '.') {
                inctile = 7;
            } else {
                inctile = to!int(tile[1 .. $]);
            }
            if (tile[0] == '+') {
                writefln("Adding locked tile: %d at pos %d, %d, id=%d\n", inctile, x, y,
                         hex.getByPos(makePoint(x, y)).id);
                done.addDone(hex.getByPos(makePoint(x, y)).id, inctile);
            } else {
                tiles[inctile]++;
            }
        }
        linei += 1;
    }

    foreach (y; 1 .. size) {
        const int ry = size - 1 + y;
        string line = lines[linei][y .. $];
        int p;
        foreach  (x; y .. size * 2 - 1) {
            string tile = line[p .. p + 2];
            p += 2;
            int inctile;
            if (tile[1] == '.') {
                inctile = 7;
            } else {
                inctile = to!int(tile[1 .. $]);
            }
            if (tile[0] == '+') {
                writefln("Adding locked tile: %d at pos %d, %d, id=%d\n", inctile,
                         x, ry, hex.getByPos(makePoint(x, ry)).id);
                done.addDone(hex.getByPos(makePoint(x, ry)).id, inctile);
            } else {
                tiles[inctile]++;
            }
        }
        linei += 1;
    }

    hex.linkNodes();
    return new Pos(hex, tiles, done);
}

private void solveFile(in string file) {
    try {
        scope Pos pos = readFile(file);
        solve(pos);
    } catch (ErrnoException e) {
        writeln("Cannot solve ", file, ", error reading");
        //e.printStackTrace(System.out); // not available?
    }
}

void main(in string[] args) {
    StopWatch sw;
    sw.start();
    foreach (f; args[1.. $]) {
        writeln(" File : ", f);
        solveFile(f);
        writeln("-------------------");
    }
    sw.stop();
    writeln("Elapsed time: ", sw.peek().msecs / 1000.0, " s");
}


Create a new paste based on this one


Comments: