<commit_before>"use strict"

const debug = require("debug")("solver")
const _ = require("lodash")

module.exports = function solve(problem) {
  const commands = []
  const drones = _.times(problem.header.ndrones, () => ({x: 0, y: 0, remainingTurns: problem.header.nturns, load: 0}))
  debug(problem.header)
  _.each(problem.orders, function (order, iorder) {
    const drone = iorder % problem.header.ndrones

    _.each(order.types, function (type) {
      const warehouse = findClosestWarehouseForProduct(problem.warehouses, drones[drone], type)
      const distanceToRun = distance(problem.warehouses[warehouse], order) + 1000;
      if(drones[drone].remainingTurns < distanceToRun) return false;

      drones[drone].remainingTurns -= distanceToRun + 1
      commands.push({command: "load", drone, warehouse, type, quantity: 1})
      problem.warehouses[warehouse].products[type] -= 1
      commands.push({command: "deliver", drone, order: iorder, type, quantity: 1})
      drones[drone] = order
    })
  })
  return commands
}

function findWarehouseForProduct(warehouses, type) {
  return _.findIndex(warehouses, function (warehouse) {
    return warehouse.products[type] > 0
  })
}

function findClosestWarehouseForProduct(warehouses, drone, type) {
  const sortedWarehouses = _.sortBy(warehouses, (warehouse) => distance(drone, warehouse))
  return findWarehouseForProduct(sortedWarehouses, type)
}

function distance(origin, destination) {
  return Math.sqrt(Math.pow(destination.x - origin.x, 2) + Math.pow(destination.y - origin.y, 2))
}
<commit_msg>feat: go load at closest warehouse<commit_after>"use strict"

const debug = require("debug")("solver")
const _ = require("lodash")

module.exports = function solve(problem) {
  const commands = []
  const drones = _.times(problem.header.ndrones, () => ({x: 0, y: 0, remainingTurns: problem.header.nturns, load: 0}))
  debug(problem.header)
  _.each(problem.orders, function (order, iorder) {
    const drone = iorder % problem.header.ndrones

    _.each(order.types, function (type) {
      const warehouse = findClosestWarehouseForProduct(problem.warehouses, drones[drone], type)
      const distanceToRun = distance(problem.warehouses[warehouse], order) + 1000;
      if(drones[drone].remainingTurns < distanceToRun) return false;

      drones[drone].remainingTurns -= distanceToRun + 1
      commands.push({command: "load", drone, warehouse, type, quantity: 1})
      problem.warehouses[warehouse].products[type] -= 1
      commands.push({command: "deliver", drone, order: iorder, type, quantity: 1})
      drones[drone] = order
    })
  })
  return commands
}

// function findWarehouseForProduct(warehouses, type) {
//   return _.findIndex(warehouses, function (warehouse) {
//     return warehouse.products[type] > 0
//   })
// }

// function findClosestWarehouseForProduct(warehouses, drone, type) {
//   const sortedWarehouses = _.sortBy(warehouses, (warehouse) => distance(drone, warehouse))
//   return findWarehouseForProduct(sortedWarehouses, type)
// }

function findClosestWarehouseForProduct(warehouses, drone, type) {
  return _(warehouses)
    .map((warehouse, index) => _.assign(warehouse, {id: index}))
    .filter((warehouse) => warehouse.products[type] > 0)
    .map(warehouse => _.assign(warehouse, {distance: distance(drone, warehouse)}))
    .sortBy("distance")
    // .tap(value => debug(_.map(_.take(value, 2), "distance")))
    .first().id
}

function distance(origin, destination) {
  return Math.sqrt(Math.pow(destination.x - origin.x, 2) + Math.pow(destination.y - origin.y, 2))
}

{'fraction_non_alphanumeric': 0.14273719563392107, 'fraction_numerical': 0.008396305625524769, 'mean_word_length': 4.473200612557427, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '587807', 'n_tokens_mistral': 1205, 'n_tokens_neox': 1114, 'n_words': 242}