-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path6.js
More file actions
55 lines (47 loc) · 1023 Bytes
/
Copy path6.js
File metadata and controls
55 lines (47 loc) · 1023 Bytes
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
let getResult = (data) => {
let arr = data[0].split('');
let x = 0;
let y = 0;
arr.forEach(command => {
switch (command) {
case 'R':
x++;
break;
case 'L':
x--;
break;
case 'D':
y--;
break;
case 'U':
y++;
break;
default:
break;
}
});
let result = '';
if (y < 0) {
result = new Array(Math.abs(y)).fill('D').join('');
if (x < 0) {
result += new Array(Math.abs(x)).fill('L').join('');
} else {
result += new Array(x).fill('R').join('');
}
} else {
if (x < 0) {
result = new Array(Math.abs(x)).fill('L').join('');
} else {
result = new Array(x).fill('R').join('');
}
result += new Array(y).fill('U').join('');
}
return result;
}
//console.log('start: ' + Date.now());
const fs = require('fs');
let fileContent = fs.readFileSync("input.txt", "utf8");
const data = fileContent.toString().trim().split("\n");
const result = getResult(data);
fs.writeFileSync("output.txt", result.toString());
//console.log('end: ' + Date.now());