1const riverSizes = input => {
2 let results = [];
3 input.forEach((row, y) => {
4 row.forEach((cell, x) => {
5 if (input[y][x] === 1) {
6 results.push(checkAdjacent(x, y, input));
7 }
8 });
9 });
10 return results;
11};
1const checkAdjacent = (x, y, input) => {
2 input[y][x] = 0; // mark cell as visited
3 let size = 1;
4
5 [[x + 1, y], [x, y + 1], [x - 1, y], [x, y - 1]].forEach(([i, j]) => {
6 // make sure we don't access a row that doesn't exist
7 // then check to see if we have a river
8 if (input[j] && input[j][i]) {
9 size += checkAdjacent(i, j, input);
10 }
11 });
12
13 return size;
14};