Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Keys and Locks solution in Clear category for Keys and Locks by JimmyCarlos
def rotate(A) -> list:
"""Rotate a 2D List 90° clockwise"""
H,W = len(A),len(A[0])
return [[A[H-C-1][R] for C in range(H)] for R in range(W)]
def keys_and_locks(doorlock,doorkey):
# Turn the doorlock and doorkey into 2-D lists
doorlock = [list(row) for row in doorlock.strip().split("\n")]
doorkey = [list(row) for row in doorkey.strip().split("\n")]
# Remove any blank rows/columns in the doorkey/doorlock with a stripTopRow/rotate combo.
for rotation in range(4):
doorlock_H,doorlock_W = len(doorlock),len(doorlock[0])
doorkey_H,doorkey_W = len(doorkey),len(doorkey[0])
while all(doorlock[0][C] == "0" for C in range(doorlock_W)):
doorlock = doorlock[1:]
while all(doorkey[0][C] == "0" for C in range(doorkey_W)):
doorkey = doorkey[1:]
doorlock,doorkey = rotate(doorlock),rotate(doorkey)
# Try the key in each of the four directions.
for rotation in range(4):
if doorkey == doorlock: return True
doorkey = rotate(doorkey)
else:
return False
Sept. 16, 2018