Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for [old] Counting Tiles by mortonfox
"use strict";
function countingTiles(radius) {
var dist = (x, y) => Math.sqrt(x * x + y * y);
var solid_count = 0;
var partial_count = 0;
for (var i = 0; i < Math.ceil(radius); ++i) {
for (var j = 0; j <= i; ++j) {
// We only need to consider a 1/8 segment of the bounding square.
// Off-diag tiles are doubled because both the tile and its twin
// (mirrored along the diagonal) can use the same calculation.
var mult = i == j ? 1 : 2;
if (dist(i + 1, j + 1) <= radius)
solid_count += mult;
else if (dist(i, j) < radius)
partial_count += mult;
}
}
// So far we've only counted tiles in a 1/4 quadrant, so multiply by 4.
return [solid_count * 4, partial_count * 4];
}
var assert = require('assert');
if (!global.is_checking) {
assert.deepEqual(countingTiles(2), [4, 12], "N=2");
assert.deepEqual(countingTiles(3), [16, 20], "N=3");
assert.deepEqual(countingTiles(2.1), [4, 20], "N=2.1");
assert.deepEqual(countingTiles(2.5), [12, 20], "N=2.5");
console.log("Coding complete? Click 'Check' to review your tests and earn cool rewards!");
}
Oct. 1, 2016