Guest User

Untitled

a guest
Dec 16th, 2022
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // preparsed input:
  2. let report = [
  3.     {sensor: {x:2302110, y:2237242}, beacon: {x:2348729, y:1239977}},
  4.     {sensor: {x:47903, y:2473047}, beacon: {x:-432198, y:2000000}},
  5.     {sensor: {x:2363579, y:1547888}, beacon: {x:2348729, y:1239977}},
  6.     {sensor: {x:3619841, y:520506}, beacon: {x:2348729, y:1239977}},
  7.     {sensor: {x:3941908, y:3526118}, beacon: {x:3772294, y:3485243}},
  8.     {sensor: {x:3206, y:1564595}, beacon: {x:-432198, y:2000000}},
  9.     {sensor: {x:3123411, y:3392077}, beacon: {x:2977835, y:3592946}},
  10.     {sensor: {x:3279053, y:3984688}, beacon: {x:2977835, y:3592946}},
  11.     {sensor: {x:2968162, y:3938490}, beacon: {x:2977835, y:3592946}},
  12.     {sensor: {x:1772120, y:2862246}, beacon: {x:2017966, y:3158243}},
  13.     {sensor: {x:3283241, y:2619168}, beacon: {x:3172577, y:2521434}},
  14.     {sensor: {x:2471642, y:3890150}, beacon: {x:2977835, y:3592946}},
  15.     {sensor: {x:3163348, y:3743489}, beacon: {x:2977835, y:3592946}},
  16.     {sensor: {x:2933313, y:2919047}, beacon: {x:3172577, y:2521434}},
  17.     {sensor: {x:2780640, y:3629927}, beacon: {x:2977835, y:3592946}},
  18.     {sensor: {x:3986978, y:2079918}, beacon: {x:3998497, y:2812428}},
  19.     {sensor: {x:315464, y:370694}, beacon: {x:-550536, y:260566}},
  20.     {sensor: {x:3957316, y:3968366}, beacon: {x:3772294, y:3485243}},
  21.     {sensor: {x:2118533, y:1074658}, beacon: {x:2348729, y:1239977}},
  22.     {sensor: {x:3494855, y:3378533}, beacon: {x:3772294, y:3485243}},
  23.     {sensor: {x:2575727, y:210553}, beacon: {x:2348729, y:1239977}},
  24.     {sensor: {x:3999990, y:2813525}, beacon: {x:3998497, y:2812428}},
  25.     {sensor: {x:3658837, y:3026912}, beacon: {x:3998497, y:2812428}},
  26.     {sensor: {x:1551619, y:1701155}, beacon: {x:2348729, y:1239977}},
  27.     {sensor: {x:2625855, y:3330422}, beacon: {x:2977835, y:3592946}},
  28.     {sensor: {x:3476946, y:2445098}, beacon: {x:3172577, y:2521434}},
  29.     {sensor: {x:2915568, y:1714113}, beacon: {x:2348729, y:1239977}},
  30.     {sensor: {x:729668, y:3723377}, beacon: {x:-997494, y:3617758}},
  31.     {sensor: {x:3631681, y:3801747}, beacon: {x:3772294, y:3485243}},
  32.     {sensor: {x:2270816, y:3197807}, beacon: {x:2017966, y:3158243}},
  33.     {sensor: {x:3999999, y:2810929}, beacon: {x:3998497, y:2812428}},
  34.     {sensor: {x:3978805, y:3296024}, beacon: {x:3772294, y:3485243}},
  35.     {sensor: {x:1054910, y:811769}, beacon: {x:2348729, y:1239977}}
  36. ];
  37.  
  38. // add the beacon-distance to each reading-object
  39. for (let dataPoint of report) {
  40.     dataPoint.distance = manhattanDistance(dataPoint.sensor, dataPoint.beacon);
  41. }
  42.  
  43. // traverse
  44. let max = 4000000;
  45. let found = false;
  46. outerLoop:for (let dataPoint of report) {
  47.     let x = dataPoint.sensor.x;
  48.     let y = dataPoint.sensor.y;
  49.     let d = dataPoint.distance + 1;
  50.     // travel the perimeter + 1
  51.     for (let i=0; i<d; i++) {
  52.         if (found||=checkPos([x+i, y-d+i])) break outerLoop;
  53.         if (found||=checkPos([x+d-i, y+i])) break outerLoop;
  54.         if (found||=checkPos([x-i, y+d-i])) break outerLoop;
  55.         if (found||=checkPos([x-d+i, y-i])) break outerLoop;
  56.     }
  57. }
  58.  
  59. function manhattanDistance(pos1, pos2) {
  60.     return Math.abs(pos1.x - pos2.x) +  Math.abs(pos1.y - pos2.y);
  61. }
  62.  
  63. function checkPos(pos) {
  64.     if (pos[0]<0) return false
  65.     if (pos[1]<0) return false
  66.     if (pos[0]>max) return false
  67.     if (pos[1]>max) return false
  68.     let cel = {x: pos[0], y: pos[1]};
  69.     for (let dataPoint of report) {
  70.         let d = manhattanDistance(dataPoint.sensor, cel);
  71.         if (d<=dataPoint.distance || manhattanDistance(cel, dataPoint.beacon)==0) {
  72.             return false
  73.         }
  74.     }    
  75.     console.log(pos[0]*4000000+pos[1]);
  76.     return true
  77. }
Advertisement
Add Comment
Please, Sign In to add comment