Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. function solution(X, Y) {
  2. // Create object
  3.  
  4. var diamond_count = 0;
  5.  
  6. var points = {};
  7. var x_axis = {};
  8. var y_axis = {};
  9.  
  10. X.forEach(function(x, i) {
  11. points['t' + i] = {
  12. 'x': x,
  13. 'y': Y[i]
  14. };
  15.  
  16. x_axis[x] = x_axis[x] || {};
  17. x_axis[x]['t' + i] = {
  18. 'x': x,
  19. 'y': Y[i]
  20. };
  21.  
  22. y_axis[Y[i]] = y_axis[Y[i]] || {};
  23. y_axis[Y[i]]['t' + i] = {
  24. 'x': x,
  25. 'y': Y[i]
  26. };
  27.  
  28. });
  29. console.log("*******************");
  30. console.log(points);
  31. console.log("*******************");
  32.  
  33. // Testing
  34. var logsC = [];
  35. logsC = Object.values(points).map(function(point) {
  36. return '(' + point.x + ',' + point.y + ')';
  37. });
  38. console.log(logsC.join());
  39.  
  40. function getAllPointsOnX(p) {
  41. return y_axis[p.y];
  42. };
  43.  
  44. function getAllPointsOnY(x) {
  45. x_axis[p.x]
  46. };
  47.  
  48. function findMiddle(a, b) {
  49. if (Math.abs(a - b) % 2 === 0) {
  50. return ((a + b) / 2);
  51. }
  52. return false
  53. };
  54.  
  55.  
  56. for (var key in points) {
  57. var logs = [];
  58. var p = points[key];
  59. // Find all on x
  60. logs.push("Looking into " + key);
  61. var nx = getAllPointsOnX(p);
  62. // Remove this one
  63. delete nx[key];
  64.  
  65. logs.push("Found " + Object.values(nx).length + " x neighbours.");
  66. logs.push("X neighbours:", nx);
  67. // Loop them
  68. for (var key_nx in nx) {
  69. // Find the middle of starting vs looped
  70. // var middle_y = p.y;
  71.  
  72. if (key_nx === key || nx[key_nx].x <= p.x) {
  73. return;
  74. };
  75.  
  76.  
  77. var middle_x;
  78. logs.push("Looking for middle between:" + nx[key_nx].x + ' and ' + p.x);
  79. if (middle_x = findMiddle(nx[key_nx].x, p.x)) {
  80. logs.push("Found middle on x:" + middle_x);
  81. // Find all on y from middle
  82. var ny = getAllPointsOnY(middle_x);
  83.  
  84. // For testing
  85. var score = {};
  86. var occurence = {};
  87. for (key_ny in ny) {
  88. var abs = Math.abs(ny[key_ny].y - p.y);
  89. score[key_ny] = {};
  90. score[key_ny] = abs;
  91.  
  92. occurence[abs] = occurence[abs] || [];
  93. occurence[abs].push(key_ny);
  94.  
  95. };
  96. var matching_occ = Object.values(occurence);
  97. matching_occ.forEach(function(a) {
  98. if (a.length === 2) {
  99. a.push(key, key_nx);
  100. a.sort();
  101. console.log("Diamond found on: ", a)
  102. console.log(logs);
  103. }
  104. });
  105. // continue
  106.  
  107. var scores = [];
  108. for (key_ny in ny) {
  109. scores.push(Math.abs(ny[key_ny].y - p.y));
  110. };
  111. // Number of diamonds with given p.x
  112. diamond_count += (scores.length - [...new Set(scores)].length);
  113. };
  114. };
  115.  
  116. };
  117. return diamond_count;
  118. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement