/* Let a n*m grid represents a population resistance to a bacterial infection.
The infection begins at a random cell, and spreads horizontally and vertically (Not diagonally) to any adjacent cell that has an infection resistance less or equals to the value of the source infected cell. Infected cells can then infect their neighbors according to the same rules.
For this exercise, assume we have access to a Cell class with the following methods:
getNeighbors(), which returns a list of neighboring cells- This method handles the grid boundaries and guarantees to never get out of bound.
getResistance(), which returns the infection resistance value of the cell
The following grid shows an example infection propagation starting at the highlighted cell with value 9
*/
const findInfectedCells = firstInfectedCell => {
let infectedCells = new Set([firstInfectedCell]);
let neighbordsToConsider = [firstInfectedCell];
while (neighbordsToConsider.length > 0) {
let currentCell = neighbordsToConsider.pop();
currentCell.getNeighbors().forEach(neighborCell => {
if (!infectedCells.has(neighborCell) && neighborCell.getResistance() < currentCell.getResistance()) {
infectedCells.add(neighborCell);
neighbordsToConsider.push(neighborCell);
}
});
}
/* const traverseNeighbors = currentCell => {
currentCell.getNeighbors().forEach(neighborCell => {
if (!infectedCells.has(neighborCell) && neighborCell.getResistance() < currentCell.getResistance()) {
infectedCells.add(neighborCell);
traverseNeighbors(neighborCell);
}
});
*/
traverseNeighbors(firstInfectedCell);
};
return infectedCells;
};
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.