/
Shadows.spec.ts
197 lines (179 loc) · 5.29 KB
/
Shadows.spec.ts
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
import * as assert from 'assert';
import * as fc from '../../src/fast-check';
// Code under tests
// Based on the CodinGame https://www.codingame.com/training/medium/shadows-of-the-knight-episode-1
class Space {
hint: string = '';
public current_x: number;
public current_y: number;
constructor(
readonly dim_x: number,
readonly dim_y: number,
private readonly solution_x: number,
private readonly solution_y: number,
private readonly initial_x: number,
private readonly initial_y: number
) {
this.current_x = initial_x;
this.current_y = initial_y;
this.update_hint();
}
update_hint() {
this.hint = '';
if (this.solution_y < this.current_y) this.hint += 'U';
else if (this.solution_y > this.current_y) this.hint += 'D';
if (this.solution_x < this.current_x) this.hint += 'L';
else if (this.solution_x > this.current_x) this.hint += 'R';
}
move(x: number, y: number) {
this.current_x = x;
this.current_y = y;
this.update_hint();
}
solved() {
return this.current_x === this.solution_x && this.current_y === this.solution_y;
}
toString() {
return `Space(grid{x:${this.dim_x},y:${this.dim_y}},solution{x:${this.solution_x},y:${this.solution_y}},initial{x:${
this.initial_x
},y:${this.initial_y}})`;
}
}
class SpaceBuilder {
dim_x = 0;
dim_y = 0;
solution_x = 0;
solution_y = 0;
current_x = 0;
current_y = 0;
withDimension(x: number, y: number) {
this.dim_x = x;
this.dim_y = y;
return this;
}
withSolution(x: number, y: number) {
this.solution_x = x;
this.solution_y = y;
return this;
}
withCurrent(x: number, y: number) {
this.current_x = x;
this.current_y = y;
return this;
}
build() {
return new Space(this.dim_x, this.dim_y, this.solution_x, this.solution_y, this.current_x, this.current_y);
}
}
function locate_in_space_bug(space: Space, rounds: number) {
let x_min = 0;
let x_max = space.dim_x;
let y_min = 0;
let y_max = space.dim_y;
for (let n = 0; n !== rounds && !space.solved(); ++n) {
if (x_min >= x_max || y_min >= y_max) {
return;
}
let x0 = space.current_x;
let y0 = space.current_y;
let hint = space.hint;
if (hint[0] == 'U') {
y_max = y0 - 1;
y0 = (y_max + y_min) / 2;
} else if (hint[0] == 'D') {
y_min = y0 + 1;
y0 = (y_max + y_min) / 2;
}
if (hint.slice(-1) == 'L') {
x_max = x0 - 1;
x0 = (x_max + x_min) / 2;
} else if (hint.slice(-1) == 'R') {
x_min = x0 + 1;
x0 = (x_max + x_min) / 2;
}
space.move(Math.floor(x0), Math.floor(y0));
}
}
function locate_in_space(space: Space, rounds: number) {
let x_min = 0;
let x_max = space.dim_x;
let y_min = 0;
let y_max = space.dim_y;
for (let n = 0; n !== rounds && !space.solved(); ++n) {
if (x_min >= x_max || y_min >= y_max) {
return;
}
let x0 = space.current_x;
let y0 = space.current_y;
let hint = space.hint;
if (hint[0] == 'U') {
y_max = y0;
y0 = (y_max + y_min) / 2;
} else if (hint[0] == 'D') {
y_min = y0 + 1;
y0 = (y_max + y_min) / 2;
}
if (hint.slice(-1) == 'L') {
x_max = x0;
x0 = (x_max + x_min) / 2;
} else if (hint.slice(-1) == 'R') {
x_min = x0 + 1;
x0 = (x_max + x_min) / 2;
}
space.move(Math.floor(x0), Math.floor(y0));
}
}
// Custom arbitrary
const SpaceArbitrary = fc
.record({
w: fc.integer(1, 1000),
h: fc.integer(1, 1000),
cx: fc.integer(1, 1000),
cy: fc.integer(1, 1000),
sx: fc.integer(1, 1000),
sy: fc.integer(1, 1000)
})
.filter(({ w, h, cx, cy, sx, sy }) => cx < w && sx < w && cy < h && sy < h)
.map(({ w, h, cx, cy, sx, sy }) =>
new SpaceBuilder()
.withDimension(w, h)
.withSolution(cx, cy)
.withCurrent(sx, sy)
.build()
)
.map((space: Space) => [space, Math.ceil(Math.log(Math.max(space.dim_x, space.dim_y)) / Math.log(2))]);
// Test
const seed = Date.now();
describe(`Shadows (seed: ${seed})`, () => {
it('Should detect an implementation issue', () => {
let failed = false;
try {
fc.assert(
fc.property(SpaceArbitrary, ([space, max_guesses]: [Space, number]) => {
locate_in_space_bug(space, max_guesses);
return space.solved();
}),
{ seed: seed }
);
} catch (err) {
failed = true;
const msg = err.message as string;
assert.ok(msg.indexOf(`seed: ${seed | 0}, path:`) !== -1, `Message contains the seed, got: ${err}`);
assert.ok(
/\[Space\(grid\{x:\d+,y:\d+\},solution\{x:\d+,y:\d+\},initial\{x:\d+,y:\d+\}\),\d+\]/.exec(msg) !== null,
`Message contains the failing entry, got: ${err}`
);
assert.ok(/failed after \d+ test/.exec(msg) !== null, `Message contains the number of tests, got: ${err}`);
}
assert.ok(failed, 'Should have failed');
});
it('Should not detect any issue', () => {
fc.assert(
fc.property(SpaceArbitrary, ([space, max_guesses]: [Space, number]) => {
locate_in_space(space, max_guesses);
return space.solved();
}),
{ seed: seed }
);
});
});