Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
With symmetry optimization solution in Clear category for Counting Tiles by brubru777
import math
def checkio(radius):
"""Count tiles.
For efficiency, this function uses several optimizations.
- We only check half of the diagonals of the top left quadrant By symmetry,
we deduce the values for the other quadrants.
- When we find the first partial tile in a diagonal, we stop the computation
because we know the followin tiles must be solid.
"""
half_side = int(math.ceil(radius))
solid, partial = 0, 0
# Tiles on the first diagonal have 4 symmetric tiles
mult = 4
# i is the index of the diagonal starting at (i, 0)
for i in range(half_side):
row, col = i, 0
while row < half_side:
row += 1
col += 1
is_inside = (half_side - row) ** 2 + (half_side - col) ** 2 < radius * radius
if is_inside:
partial += mult
solid += mult * (half_side - row)
break
# Tiles on the other diagonals have 8 symmetric tiles
mult = 8
return [solid, partial]
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
assert checkio(2) == [4, 12], "N=2"
assert checkio(3) == [16, 20], "N=3"
assert checkio(2.1) == [4, 20], "N=2.1"
assert checkio(2.5) == [12, 20], "N=2.5"
Jan. 3, 2019