aboutsummaryrefslogtreecommitdiff
path: root/challenge-118/paulo-custodio/d/ch_2.d
blob: eed57ac24d5acf84249087757c49b25d8e956e85 (plain)
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
/*
Challenge 118

TASK #2 - Adventure of Knight
Submitted by : Cheok - Yin Fung
A knight is restricted to move on an 8x8 chessboard.The knight is denoted by
N and its way of movement is the same as what it is defined in Chess.
* represents an empty square. x represents a square with treasure.

The Knight's movement is unique.It may move two squares vertically and one
square horizontally, or two squares horizontally and one square vertically
(with both forming the shape of an L).

There are 6 squares with treasures.

Write a script to find the path such that Knight can capture all treasures.
The Knight can start from the top - left square.

  a b c d e f g h
8 N * * * * * * * 8
7 * * * * * * * * 7
6 * * * * x * * * 6
5 * * * * * * * * 5
4 * * x * * * * * 4
3 * x * * * * * * 3
2 x x * * * * * * 2
1 * x * * * * * * 1
  a b c d e f g h

https://en.m.wikipedia.org/wiki/Knight%27s_tour
*/

import std.stdio;
import std.conv;
import core.stdc.stdlib : exit;

enum NROWS     = 8;
enum NCOLS     = 8;
enum MAX_MOVES = 8;

struct Board {
    ubyte[NROWS] cells;

    void clear() {
        cells[] = 0;
    }

    bool empty() const {
        for (int i = 0; i < NROWS; i++)
            if (cells[i] != 0)
                return false;
        return true;
    }

    void set(int row, int col) {
        assert(row >= 0 && row < NROWS);
        assert(col >= 0 && col < NCOLS);
        int mask = 1 << col;
        cells[row] |= mask;
    }

    void reset(int row, int col) {
        assert(row >= 0 && row < NROWS);
        assert(col >= 0 && col < NCOLS);
        int mask = 1 << col;
        cells[row] &= ~mask;
    }

    bool peek(int row, int col) const {
        assert(row >= 0 && row < NROWS);
        assert(col >= 0 && col < NCOLS);
        int mask = 1 << col;
        return (cells[row] & mask) == 0 ? false : true;
    }
}

struct Game {
    int row, col;
    Board visited, treasure;
    string path;

    void clear() {
        row = col = 0;
        visited.clear();
        treasure.clear();
        path = "";
    }

    void set_visited(int row, int col) {
        visited.set(row, col);
    }

    bool peek_visited(int row, int col) const {
        return visited.peek(row, col);
    }

    void set_treasure(int row, int col) {
        treasure.set(row, col);
    }

    void clear_treasure(int row, int col) {
        treasure.reset(row, col);
    }

    bool peek_treasure(int row, int col) const {
        return treasure.peek(row, col);
    }

    bool done() const {
        return treasure.empty();
    }

    void move(int row, int col) {
        set_visited(row, col);
        clear_treasure(row, col);
        path ~= 'a' + col;
        path ~= to!string(NROWS - row);
        path ~= " ";
        this.row = row;
        this.col = col;
    }

    void print() {
        writeln("  a b c d e f g h");
        for (int row = 0; row < NROWS; row++) {
            write(NROWS - row);
            for (int col = 0; col < NCOLS; col++) {
                if (peek_treasure(row, col))
                    write(" x");
                else if (this.row == row && this.col == col)
                    write(" N");
                else if (peek_visited(row, col))
                    write(" .");
                else
                    write(" *");
            }
            writeln(" ", NROWS - row);
        }
        writeln("  a b c d e f g h");
        writeln("");
        writeln("> ", path);
    }

    void parse_header() {
        string line;
        if ((line = readln()) is null) {
            stderr.writeln("expected header or footer");
            exit(1);
        }
        auto p = 0;
        while (p < line.length && line[p] == ' ') p++;
        if (line[p .. p+15] != "a b c d e f g h") {
            stderr.writeln("invalid header or footer");
            exit(1);
        }
    }

    void parse_row(int row) {
        string line;
        if ((line = readln()) is null) {
            stderr.writeln("expected row ", NROWS - row);
            exit(1);
        }
        auto p = 0;
        while (p < line.length && line[p] == ' ') p++;
        if (line[p] - '0' != NROWS - row) {
            stderr.writeln("invalid row ", NROWS - row);
            exit(1);
        }
        p++;
        for (int col = 0; col < NCOLS; col++) {
            while (p < line.length && line[p] == ' ') p++;
            switch (line[p]) {
                case 'N': move(row, col); break;
                case 'x': set_treasure(row, col); break;
                case '*': break;
                default:
                    stderr.writeln("invalid row ", NROWS - row);
                    exit(1);
            }
            p++;
        }
    }

    void parse() {
        clear();
        parse_header();
        for (int row = 0; row < NROWS; row++)
            parse_row(row);
        parse_header();
    }

}

struct Move {
    int row, col, num_moves;
}

void try_move(Move[]* moves, Game* game, int nrow, int ncol) {
    if (nrow < 0 || nrow >= NROWS) return;
    if (ncol < 0 || ncol >= NCOLS) return;
    if (game.peek_visited(nrow, ncol)) return;

    *moves = *moves ~ [Move(nrow, ncol, 0)];
}

void try_num_moves(Move[]* moves, Game* game, int row, int col) {
    *moves = [];
    try_move(moves, game, row + 2, col + 1);
    try_move(moves, game, row + 2, col - 1);
    try_move(moves, game, row - 2, col + 1);
    try_move(moves, game, row - 2, col - 1);
    try_move(moves, game, row + 1, col + 2);
    try_move(moves, game, row - 1, col + 2);
    try_move(moves, game, row + 1, col - 2);
    try_move(moves, game, row - 1, col - 2);
}

Move[] next_moves(Game* game) {
    Move[] moves;

    // try first step
    try_num_moves(&moves, game, game.row, game.col);

    // for each target location count number of further steps
    int min_moves = MAX_MOVES + 1;
    for (int i = 0; i < moves.length; i++) {
        Move[] temp_moves;
        try_num_moves(&temp_moves, game, moves[i].row, moves[i].col);
        moves[i].num_moves = cast(int)temp_moves.length;
        if (min_moves > moves[i].num_moves) min_moves = moves[i].num_moves;