Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
v2: unoriented lines, generate square lines solution in Clear category for The Square Chest by Phil15
import itertools as it
def checkio(lines, nb_rows=4, nb_cols=4):
"""Return the quantity of squares formed by the given lines."""
# Available unoriented lines.
# frozenset make a line unoriented and hashable hence storable in a set.
edges = {frozenset(divmod(nb - 1, nb_cols) for nb in AB) for AB in lines}
return sum(
edges.issuperset(_square_lines(size, start))
for size in range(1, min(nb_rows, nb_cols) + 1)
for start in it.product(range(nb_rows - size), range(nb_cols - size))
)
def _square_lines(size, start):
"""Generate required lines for a square of given size and start."""
end = start[0] + size, start[1] + size
for d, (i, j) in (1, start), (-1, end):
for di, dj in (0, d), (d, 0):
# Points on one of the four sides.
side = ((i + k * di, j + k * dj) for k in range(size + 1))
# Generate all the unoriented lines that form this side.
yield from map(frozenset, pairwise(side))
# https://docs.python.org/3/library/itertools.html#itertools-recipes
def pairwise(iterable):
"""s -> (s0, s1), (s1, s2), (s2, s3), ..."""
a, b = it.tee(iterable)
next(b, None)
return zip(a, b)
Nov. 26, 2020
Comments: