-
Notifications
You must be signed in to change notification settings - Fork 1
/
15.js
67 lines (54 loc) · 1.44 KB
/
15.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
65
66
67
let getResult = (data) => {
let parse_ints = str => {
return str.split(' ').filter(val => val != '').map(val => parseInt(val.trim()));
}
const TYPE_X = 'X';
const TYPE_Y = 'Y';
let getSum = (type, m) => {
let sum = 0;
for (let j = 0; j < n; j++) {
if (type == TYPE_X) {
sum += Math.abs(coords[j][0] - m);
} else if (type == TYPE_Y) {
sum += Math.abs(coords[j][1] - m);
}
}
return sum;
}
let check = (m, type) => {
if (getSum(type, m) < getSum(type, m-1)) return true;
if (getSum(type, m) < getSum(type, m-1)) return true;
return false;
}
let rbinsearch = (l, r, check, checkparams) => {
while (l < r) {
m = Math.floor((l + r + 1) / 2);
if (check(m, checkparams)) {
l = m;
} else {
r = m - 1;
}
}
return l;
}
let n = parseInt(data[0]);
let coords = [];
let minx = maxx = miny = maxy = 0;
for (let i = 1; i <= n; i++) {
let [x, y] = parse_ints(data[i]);
minx = Math.min(minx, x);
maxx = Math.max(maxx, x);
miny = Math.min(miny, y);
maxy = Math.max(maxy, y);
coords.push([x, y]);
}
let x_goal = rbinsearch(minx, maxx, check, TYPE_X);
let y_goal = rbinsearch(miny, maxy, check, TYPE_Y);
return x_goal + y_goal;
}
const { count } = require('console');
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());