JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/svg.js/2.2.4/svg.js"></script>
<h1>Rescue Routes</h1>
<div id="drawing"></div>
JavaScript
function buildInputData(seats, peopleCount) {
var i, people = [];
for (i = 0; i < peopleCount; i++) {
people.push({x: getRandomCoordinate(), y: getRandomCoordinate()});
}
return {seats: seats, people: people};
}
function buildOutputData(inputData) {
var i, res = [], indexes = [];
for (i = 0; i < inputData.people.length; i++) {
indexes.push(i);
if ( indexes.length == inputData.seats ) {
res.push({x: getRandomCoordinate(), y: getRandomCoordinate(), people: indexes});
indexes = [];
}
}
if ( indexes.length ) {
res.push({x: getRandomCoordinate(), y: getRandomCoordinate(), people: indexes});
}
return res;
}
function getRandomCoordinate() {
return Math.floor(Math.random() * 10000) + 1;
}
function checkResult(inputData, outputData) {
var i, j, index, item, optimalCount, people = [];
function assert(condition, msg) {
if ( !condition ) {
throw msg;
}
}
try {
// check number of helicopters
optimalCount = Math.ceil(inputData.people.length / inputData.seats);
assert(optimalCount === outputData.length, "Not optimal number of helicopters " + outputData.length);
// check helicopter one by one
for (i = 0; i < outputData.length; i++) {
item = outputData[i];
// check coordinates
assert(item.x === Math.floor(item.x), "Not integer X coordinate " + item.x);
assert(item.y === Math.floor(item.y), "Not integer Y coordinate " + item.y);
assert(item.x >= 0, "Negative X coordinate " + item.x);
assert(item.y >= 0, "Negative Y coordinate " + item.y);
// check overcrowded helicopter
assert(item.people.length <= inputData.seats, "Helicopter overcrowded");
// check unique of people
for (j = 0; j < item.people.length; j++) {
index = item.people[j];
assert((index >= 0 && index < inputData.people.length), "Wrong index: " + index);
assert(!people[index], "The same index twice: " + index);
people[index]...