Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // preparsed input:
- let report = [
- {sensor: {x:2302110, y:2237242}, beacon: {x:2348729, y:1239977}},
- {sensor: {x:47903, y:2473047}, beacon: {x:-432198, y:2000000}},
- {sensor: {x:2363579, y:1547888}, beacon: {x:2348729, y:1239977}},
- {sensor: {x:3619841, y:520506}, beacon: {x:2348729, y:1239977}},
- {sensor: {x:3941908, y:3526118}, beacon: {x:3772294, y:3485243}},
- {sensor: {x:3206, y:1564595}, beacon: {x:-432198, y:2000000}},
- {sensor: {x:3123411, y:3392077}, beacon: {x:2977835, y:3592946}},
- {sensor: {x:3279053, y:3984688}, beacon: {x:2977835, y:3592946}},
- {sensor: {x:2968162, y:3938490}, beacon: {x:2977835, y:3592946}},
- {sensor: {x:1772120, y:2862246}, beacon: {x:2017966, y:3158243}},
- {sensor: {x:3283241, y:2619168}, beacon: {x:3172577, y:2521434}},
- {sensor: {x:2471642, y:3890150}, beacon: {x:2977835, y:3592946}},
- {sensor: {x:3163348, y:3743489}, beacon: {x:2977835, y:3592946}},
- {sensor: {x:2933313, y:2919047}, beacon: {x:3172577, y:2521434}},
- {sensor: {x:2780640, y:3629927}, beacon: {x:2977835, y:3592946}},
- {sensor: {x:3986978, y:2079918}, beacon: {x:3998497, y:2812428}},
- {sensor: {x:315464, y:370694}, beacon: {x:-550536, y:260566}},
- {sensor: {x:3957316, y:3968366}, beacon: {x:3772294, y:3485243}},
- {sensor: {x:2118533, y:1074658}, beacon: {x:2348729, y:1239977}},
- {sensor: {x:3494855, y:3378533}, beacon: {x:3772294, y:3485243}},
- {sensor: {x:2575727, y:210553}, beacon: {x:2348729, y:1239977}},
- {sensor: {x:3999990, y:2813525}, beacon: {x:3998497, y:2812428}},
- {sensor: {x:3658837, y:3026912}, beacon: {x:3998497, y:2812428}},
- {sensor: {x:1551619, y:1701155}, beacon: {x:2348729, y:1239977}},
- {sensor: {x:2625855, y:3330422}, beacon: {x:2977835, y:3592946}},
- {sensor: {x:3476946, y:2445098}, beacon: {x:3172577, y:2521434}},
- {sensor: {x:2915568, y:1714113}, beacon: {x:2348729, y:1239977}},
- {sensor: {x:729668, y:3723377}, beacon: {x:-997494, y:3617758}},
- {sensor: {x:3631681, y:3801747}, beacon: {x:3772294, y:3485243}},
- {sensor: {x:2270816, y:3197807}, beacon: {x:2017966, y:3158243}},
- {sensor: {x:3999999, y:2810929}, beacon: {x:3998497, y:2812428}},
- {sensor: {x:3978805, y:3296024}, beacon: {x:3772294, y:3485243}},
- {sensor: {x:1054910, y:811769}, beacon: {x:2348729, y:1239977}}
- ];
- // add the beacon-distance to each reading-object
- for (let dataPoint of report) {
- dataPoint.distance = manhattanDistance(dataPoint.sensor, dataPoint.beacon);
- }
- // traverse
- let max = 4000000;
- let found = false;
- outerLoop:for (let dataPoint of report) {
- let x = dataPoint.sensor.x;
- let y = dataPoint.sensor.y;
- let d = dataPoint.distance + 1;
- // travel the perimeter + 1
- for (let i=0; i<d; i++) {
- if (found||=checkPos([x+i, y-d+i])) break outerLoop;
- if (found||=checkPos([x+d-i, y+i])) break outerLoop;
- if (found||=checkPos([x-i, y+d-i])) break outerLoop;
- if (found||=checkPos([x-d+i, y-i])) break outerLoop;
- }
- }
- function manhattanDistance(pos1, pos2) {
- return Math.abs(pos1.x - pos2.x) + Math.abs(pos1.y - pos2.y);
- }
- function checkPos(pos) {
- if (pos[0]<0) return false
- if (pos[1]<0) return false
- if (pos[0]>max) return false
- if (pos[1]>max) return false
- let cel = {x: pos[0], y: pos[1]};
- for (let dataPoint of report) {
- let d = manhattanDistance(dataPoint.sensor, cel);
- if (d<=dataPoint.distance || manhattanDistance(cel, dataPoint.beacon)==0) {
- return false
- }
- }
- console.log(pos[0]*4000000+pos[1]);
- return true
- }
Advertisement
Add Comment
Please, Sign In to add comment