Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for [old] Radiation Search by Sim0000
"use strict";
function area(data, x, y, c) {
ym = data.length
xm = data[0].length
offset = [[0,-1], [-1,0], [1,0], [0,1]]
q = [[x, y]]
count = 0
while(q.length){
x = q[0][0]
y = q[0][1]
q.shift()
if(x < 0 || xm <= x || y < 0 || ym <= y || data[y][x] != c) continue
data[y][x] = 0
count++
for(d of offset){
q.push([x - d[0], y - d[1]])
}
}
return count
}
function radiationSearch(data) {
n = data.length
result = []
for(y in data){
for(x in data[0]){
c = data[y][x]
if(c){
d = area(data, x, y, c)
result.push([d, c])
}
}
}
return result.reduce((p, c) => p[0] > c[0] ? p : c)
}
var assert = require('assert');
if (!global.is_checking) {
assert.equal(radiationSearch([
[1, 2, 3, 4, 5],
[1, 1, 1, 2, 3],
[1, 1, 1, 2, 2],
[1, 2, 2, 2, 1],
[1, 1, 1, 1, 1]
]), [14, 1], "14 of 1");
assert.equal(radiationSearch([
[2, 1, 2, 2, 2, 4],
[2, 5, 2, 2, 2, 2],
[2, 5, 4, 2, 2, 2],
[2, 5, 2, 2, 4, 2],
[2, 4, 2, 2, 2, 2],
[2, 2, 4, 4, 2, 2]
]), [19, 2], "19 of 2");
console.log("Coding complete? Click 'Check' to review your tests and earn cool rewards!");
}
July 8, 2016