title: Allocate bikes to people - First Priority (Closest bike to closest person)
body: <p>Passing in a grid to a function with bikes and person at locations</p>
  
  <pre><code>[ 'c' , '_' ,'A' ,'_', '_' , '_']
  [ '_' , '_' ,'a' ,'_', '_' , '_']
  [ '_' , '_' ,'_' ,'_', 'b' , '_']
  [ '_' , '_' ,'_' ,'_', '_' , '_']
  [ 'D' , 'd' ,'_' ,'_', '_' , 'B']
  [ '_' , '_' ,'_' ,'C', '_' , '_']
  </code></pre>
  
  <p><strong>Output:</strong> Something like this <code>[A:1, B:3, C:8, D:1]</code> </p>
  
  <blockquote>
    <p>Where A is the person and 1 is the step required to travel to get to the bike. </p>
  </blockquote>
  
  <p><strong>Criterias:</strong></p>
  
  <ol>
  <li>Closest person to the bike, get the bike at the first priority.</li>
  <li>Single bike can't be assigned to 2 individuals</li>
  <li>Distance of a bike from one individual will never be equal to distance of the same bike from a different individual.</li>
  <li>Distances can be equal, but 2 different bikes and 2 different individuals</li>
  </ol>
  
  <p>I feel like Graphical representation might make more sense hence </p>
  
  <p><a href="https://i.stack.imgur.com/vRwPb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vRwPb.png" alt="enter image description here"></a></p>
  
  <hr>
  
  <p><strong>My Approach:</strong></p>
  
  <ol>
  <li><p>Find the location of Bikes and Person and store them in an Array.</p>
  
  <p><code>person = [[0,2],[4,0],[4,5],[5,3]], bikes = [[0,0],[1,2],[2,4],[4,1]];</code></p></li>
  <li><p>As shortest path will be 1, start removing the bikes and person from the Array 
  who has the shortest path as 1 and keep incrementing the shortest path by 1.
  And store the person and bike into results array.</p></li>
  <li><p>Need to keep doing step # 2 till our Person's Array is empty</p></li>
  </ol>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function findBikesForPeople(grid) {
  
    let row_length = grid.length;
    let col_length = grid[0].length;
    var bikes = [],
      person = [];
  
    for (var row = 0; row &lt; row_length; row++) {
      for (var col = 0; col &lt; col_length; col++) {
        if (grid[row][col] === 'B') {
          bikes.push([row, col]);
        }
        if (grid[row][col] === 'P') {
          person.push([row, col]);
        }
      }
    }
  
    var distances = (bikes, person) =&gt; {
      var dist = [];
      person.map((single) =&gt; {
        var inner = [];
        bikes.map((bike) =&gt; {
          inner.push(check_distance(single, bike));
        })
        dist.push(inner);
      })
      return dist;
    }
  
  
    //This isn't right
    var AllocateBikes = (distances) =&gt; {
      //var result = [];
      //var min = 1;
      //var increment = 0;
      //  let people = distances.length;
      //let bikeCount = distances[0].length;
      //while (people &gt; 0) {
      //  if (Math.min(...distances[]))
      // }
      return distances;
    }
  
    function check_distance(a, b) {
      return Math.abs(b[1] - a[1]) + Math.abs(b[0] - a[0]);
    }
  
    let distance_between = distances(bikes, person);
    console.log(AllocateBikes(distance_between));
  
  }
  var grid = [
    ['P', '_', 'B', '_', '_'],
    ['_', '_', '_', '_', 'B'],
    ['_', '_', '_', '_', '_'],
    ['_', 'P', '_', '_', '_'],
    ['_', '_', '_', '_', 'B']
  ];
  
  findBikesForPeople(grid);</code></pre>
  </div>
  </div>
  </p>