Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Brute Force solution in Clear category for The Square Chest by Cordivae
def square_test(lines_list, start, size):
"""Test to see if a square of size 'size' is found at start"""
for segment in range(size):
upper = [start+segment, start+segment+1]
left = [start+segment*4, start+segment*4+4]
bot = [start+segment+size*4, start+segment+size*4+1]
right = [start+size+segment*4, start+size+segment*4+4]
for line in [upper, left, bot, right]:
rev = [line[1], line[0]]
if line not in lines_list and rev not in lines_list:
return False
return True
def checkio(lines_list):
"""Return the quantity of squares"""
total = 0
for size in range(1, 4):
run = range(4-size)
starts = (x + 4*y + 1 for x in run for y in run)
for start in starts:
if square_test(lines_list, start, size):
total += 1
return total
if __name__ == '__main__':
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"
Oct. 12, 2017