-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoboScript#4.js
More file actions
280 lines (244 loc) · 8.19 KB
/
RoboScript#4.js
File metadata and controls
280 lines (244 loc) · 8.19 KB
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
// https://www.codewars.com/kata/594b898169c1d644f900002e/solutions/javascript
function execute(code) {
try {
const instructions = parse(code);
console.log(instructions);
return makeResult(makePaths(instructions)).map(e => e.join('')).join('\r\n');
} catch (e) {
console.log(e);
throw e;
}
function makePaths(instructions) {
let direction = 0;
let lastPos = [0, 0];
let paths = [lastPos];
console.log(`initState: ${instructions}`);
for (let index = 0; index < instructions.length; index++) {
const instruction = instructions[index];
switch (instruction) {
case 'L':
direction = (direction + 3) % 4;
break;
case 'R':
direction = (direction + 1) % 4;
break;
case 'F':
forward();
break;
default:
break;
}
}
return paths;
function forward() {
const [x, y] = lastPos;
const nextPos = [[x + 1, y], [x, y + 1], [x - 1, y], [x, y - 1]];
lastPos = nextPos[direction];
paths.push(lastPos);
}
}
function makeResult(paths) {
const { left, right, up, down } = paths.reduce((a, [x, y]) => {
a.left = Math.min(a.left, x);
a.right = Math.max(a.right - 1, x) + 1;
a.up = Math.min(a.up, y);
a.down = Math.max(a.down - 1, y) + 1;
return a;
}, { left: 0, right: 0, up: 0, down: 0 });
const width = right - left;
const height = down - up;
const offsetX = 0 - left;
const offsetY = 0 - up;
console.log(width, height, offsetX, offsetY);
const array = Array(height).fill(0).map(_ => Array(width).fill(' '));
paths.map(([x, y]) => [x + offsetX, y + offsetY]).forEach(([x, y]) => {
array[y][x] = '*';
});
return array;
}
function parse(code) {
console.log({ code });
const env = new Map();
code = handlePatterns(code);
if (code.length == 0) return '';
let tokens = tokenizer(code);
console.log({ tokens });
let tokenIndex = 0;
let nextToken = tokens[tokenIndex];
return expr();
function handlePatterns(code) {
console.log('handlePatterns:', code);
const patternRegex = /p(\d*)(.*?)q/g;
let match;
while ((match = patternRegex.exec(code)) !== null) {
const [_, name, content] = match;
console.log({ name, content });
if (env.has(name)) {
throw `duplicate pattern: p${name}`;
}
env.set(name, content);
}
/*
for (const [_, name, content] of code.matchAll(patternRegex)) {
// console.log(pattern);
if (env.has(name)) {
throw `duplicate pattern: p${name}`;
}
env.set(name, content);
}
*/
code = code.replace(patternRegex, '');
console.log({ code });
console.log(env);
code = replacePatternsRecursive(code, new Set());
console.log({ code });
return code;
function replacePatternsRecursive(str, topPatterns) {
return str.replace(/P(\d+)/g, (match, p) => {
console.log({ match, p, topPatterns });
if (topPatterns.has(p)) {
throw `infinite recursion: '${p}'`;
}
const content = env.get(p);
console.log({ content });
// const topPatterns = new Set(topPatterns);
// topPatterns.add(p);
// const result = replacePatternsRecursive(content, topPatterns);
const set = new Set(topPatterns);
set.add(p);
console.log({ set });
const result = replacePatternsRecursive(content, set);
console.log({ result });
return result;
});
}
}
function tokenizer(code) {
const tokens = [];
let temp = "";
for (let index = 0; index < code.length; index++) {
const char = code[index];
if (/[0-9]/.test(char)) {
temp += char;
}
else {
if (temp.length > 0) {
tokens.push(Number(temp));
temp = "";
}
tokens.push(char);
}
}
if (temp.length > 0) {
tokens.push(Number(temp));
}
return tokens;
}
function match(t) {
switch (t) {
case undefined:
case nextToken:
tokenIndex++;
nextToken = tokenIndex < tokens.length ? tokens[tokenIndex] : '$';
return true;
default:
return false;
}
}
function expr() {
let e = "";
while (nextToken != ')' && nextToken != '$' && nextToken != 'q') {
e += term();
}
return e;
}
function term() {
const f = factor();
if (typeof nextToken == 'number') {
let t = f.repeat(nextToken);
match();
return t;
}
else {
return f;
}
}
function factor() {
switch (nextToken) {
case 'F':
case 'L':
case 'R': {
const token = nextToken;
match();
return token;
}
case '(': return paren();
default:
throw `unexpected: ${nextToken}`;
}
}
function paren() {
match();
const e = expr();
if (match(")")) {
return e;
}
else {
throw "')' expected!";
}
}
}
}
function testPrint(...args) {
console.log(...args);
}
// testPrint(1,2,3);
input1 = "FFFFFLFFFFFLFFFFFLFFFFFL";
input2 = "LFFFFFRFFFRFFFRFFFFFFF";
input3 = "LF5RF3RF3RF7";
// input4 = "F4L((F4R)2(F4L)2)2(F4R)2F4";
input4 = "LF5(RF3)(RF3R)F7";
input5 = "(L(F5(RF3))(((R(F3R)F7))))";
input6 = "F4L(F4RF4RF4LF4L)2F4RF4RF4";
input7 = "F4L((F4R)2(F4L)2)2(F4R)2F4";
input8 = "F4L((F4R)22(F412L)2)250(F4R)2F4";
stmt1 = '(F2LF2R)2FRF4L(F2LF2R)2(FRFL)4(F2LF2R)2';
stmt2 = 'p0(F2LF2R)2q';
stmt3 = 'p312(F2LF2R)2q';
stmt4 = 'p0(F2LF2R)2qP0';
stmt5 = 'p312(F2LF2R)2qP312';
stmt6 = 'P0p0(F2LF2R)2q';
stmt7 = 'P312p312(F2LF2R)2q';
stmt8 = 'F3P0Lp0(F2LF2R)2qF2';
stmt9 = '(P0)2p0F2LF2RqP0';
stmt10 = 'p0(F2LF2R)2qP1';
stmt11 = 'P0p312(F2LF2R)2q';
stmt12 = 'P312';
stmt13 = 'P1P2p1F2Lqp2F2RqP2P1';
stmt14 = 'p1F2Lqp2F2Rqp3P1(P2)2P1q(P3)3';
stmt15 = 'p1F2Lqp1(F3LF4R)5qp2F2Rqp3P1(P2)2P1q(P3)3';
// stmt15_ = 'p10F2Lqp1(F3LF4R)5qp2F2Rqp3P1(P2)2P1q(P3)3PP';
stmt15_ = 'p10F2Lqp1(F3LF4R)5qp2F2Rqp3P1(P2)2P1q(P3)3';
stmt16 = 'p1F2RP1F2LqP1';
stmt17 = 'p1F2LP2qp2F2RP1qP1';
// console.log(execute(input1));
// console.log(execute(input2));
// console.log(execute(input3));
// console.log(parse(input4));
// console.log(parse(input7));
// console.log(execute(input4));
// console.log(execute(input5));
// console.log(execute(input6));
// console.log(execute(input7));
// console.log(execute(input8));
console.log(execute(stmt1));
// execute(stmt1);
// console.log(execute(stmt2));
// console.log(execute(stmt4));
// console.log(execute(stmt15));
// console.log(execute(stmt15_));
// console.log(tokenizer(input8));
// console.log(tokenizer(stmt7));
// execute("p1F2RP1F2LqP1"); // throws
// execute("p1F2LP2qp2F2RP1qP1"); // throws
// testPrint(execute("p1F2RP1F2Lq")); // does not throw