Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for The Square Chest by igor.v.dudenko
from typing import List
def checkio(lines_list: List[List[int]]) -> int:
def check_square(start, size):
sx, sy = start
ex, ey = sx + size * 2, sy + size * 2
for d in range(0, size * 2):
if not (matrix[sx][sy + d] and
matrix[sx + d][sy] and
matrix[ex][sy + d] and
matrix[sx + d][ey]):
return 0
return 1
matrix = [[0]*7 for i in range(8)]
for lx, le in lines_list:
sx, sy = (lx - 1) // 4 * 2, (lx - 1) % 4 * 2
ex, ey = (le - 1) // 4 * 2, (le - 1) % 4 * 2
mx, my = int(ex - (ex - sx) / 2), int(ey - (ey - sy) / 2)
matrix[sx][sy] = matrix[ex][ey] = matrix[mx][my] = 1
count = 0
for x in range(3):
for y in range(3):
for size in range(1, min(3 - x, 3 - y) + 1):
count += check_square((x * 2, y * 2), size)
return count
if __name__ == '__main__':
print("Example:")
print(checkio([[1, 2], [3, 4], [1, 5], [2, 6], [4, 8], [5, 6], [6, 7],
[7, 8], [6, 10], [7, 11], [8, 12], [10, 11],
[10, 14], [12, 16], [14, 15], [15, 16]]))
assert (checkio([[1, 2], [3, 4], [1, 5], [2, 6], [4, 8], [5, 6], [6, 7],
[7, 8], [6, 10], [7, 11], [8, 12], [10, 11],
[10, 14], [12, 16], [14, 15], [15, 16]]) == 3), "First, from description"
assert (checkio([[1, 2], [2, 3], [3, 4], [1, 5], [4, 8],
[6, 7], [5, 9], [6, 10], [7, 11], [8, 12],
[9, 13], [10, 11], [12, 16], [13, 14], [14, 15], [15, 16]]) == 2), "Second, from description"
assert (checkio([[1, 2], [1, 5], [2, 6], [5, 6]]) == 1), "Third, one small square"
assert (checkio([[1, 2], [1, 5], [2, 6], [5, 9], [6, 10], [9, 10]]) == 0), "Fourth, it's not square"
assert (checkio([[16, 15], [16, 12], [15, 11], [11, 10],
[10, 14], [14, 13], [13, 9]]) == 0), "Fifth, snake"
print("Coding complete? Click 'Check' to earn cool rewards!")
Feb. 20, 2019