-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path!1004_littlePrince.js
64 lines (54 loc) · 1.37 KB
/
!1004_littlePrince.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// 1004 어린 왕자
let fs = require("fs");
let [n, ...tcs] = fs.readFileSync("input.txt").toString().trim().split("\n");
let mapping = {
prince: [],
rose: [],
};
let prince;
let rose;
let leng;
for (let i = 0; i < tcs.length; i++) {
const spl = tcs[i].split(" ");
if (spl.length === 4) {
if (leng === 0) {
console.log(mapping.prince, "prince");
console.log(mapping.rose, "rose");
console.log(countLayer());
console.log("-----------");
}
const [x1, y1, x2, y2] = spl;
prince = [+x1, +y1];
rose = [+x2, +y2];
console.log(prince, rose, "prince/rose by 4");
} else if (spl.length === 1) {
leng = +tcs[i];
} else {
const [x1, y1, r] = spl;
markBoundary(x1, y1, r);
leng--;
}
}
if (leng === 0) {
console.log(countLayer());
}
function markBoundary(x1, y1, r) {
let leftTop = [+x1 - +r, +y1 - +r];
let rightBottom = [+x1 + +r, +y1 + +r];
for (let i = leftTop[0]; i <= rightBottom[0]; i++) {
for (let j = leftTop[1]; j <= rightBottom[1]; j++) {
if (prince[0] === i && prince[1] === j) {
mapping.prince.push(`${x1}_${y1}_${r}`);
}
if (rose[0] === i && rose[1] === j) {
mapping.rose.push(`${x1}_${y1}_${r}`);
}
}
}
}
function countLayer() {
const set = new Set([...mapping.prince, ...mapping.rose]);
mapping.prince = [];
mapping.rose = [];
return set.size;
}