-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJavascript.js
47 lines (47 loc) · 1.31 KB
/
Javascript.js
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
const numberSnails = parseInt(readline());
let listSnail = [];
let listEnd = [];
for (let i = 0; i < numberSnails; i++) {
const speedSnail = parseInt(readline());
listSnail.push(speedSnail);
}
let map = [];
const mapHeight = parseInt(readline());
const mapWidth = parseInt(readline());
for (let i = 0; i < mapHeight; i++) {
const ROW = readline();
for (let j = 0; j < mapWidth; j++) {
if (ROW[j] != "*" && ROW[j] != "#") {
listSnail[parseInt(ROW[j]) - 1] = { id: ROW[j], i: i, j: j, speed: listSnail[parseInt(ROW[j]) - 1] };
} else if (ROW[j] == "#") {
listEnd.push({ i: i, j: j });
}
}
map.push(ROW.split(""));
}
let turnForWin = [];
for (let i = 0; i < listSnail.length; i++) {
turnForWin.push({ id: listSnail[i].id, turn: simulateRun(listSnail[i], listEnd) });
}
turnForWin.sort((a, b) => {
return a.turn - b.turn;
});
console.log(turnForWin[0].id);
function simulateRun(snail, listEnd) {
let d = Infinity;
let nearestEnd = null;
for (let i = 0; i < listEnd.length; i++) {
let _d = Math.abs(snail.i - listEnd[i].i) + Math.abs(snail.j - listEnd[i].j);
if (_d < d) {
d = _d;
nearestEnd = listEnd[i];
}
}
let count = 0;
d = Math.abs(snail.i - nearestEnd.i) + Math.abs(snail.j - nearestEnd.j);
while (d > 0) {
count++;
d -= snail.speed;
}
return count;
}