Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Landing Site by kdim
from typing import Set
def landing_site(obstacles: Set[str]) -> Set[str]:
def neighbours(cell):
if ord(cell[0]) % 2:
directions = {(0, -1), (1, -1), (1, 0), (0, 1), (-1, 0), (-1, -1)}
else:
directions = {(0, -1), (1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0)}
result = list()
for i, j in directions:
s, n = ord(cell[0]) + i, int(cell[1]) + j
if 64 <= s <= 77 and 0 <= n <= 10:
result.append(chr(s) + str(n))
return set(result)
def radius(r, cells):
if cells & obstacles:
return r
setcells = set("".join(cell for cell in cells))
if len(setcells & GRID) != len(setcells):
return r
cellneighbours = set()
for cell in cells:
cellneighbours |= neighbours(cell)
return radius(r + 1, cellneighbours - cells)
GRID = set('ABCDEFGHIJKL123456789')
result = list()
for i in range(2, 12):
for j in range(2, 9):
cell = set([chr(64 + i) + str(j)])
result.append((radius(0, cell), cell))
maxradius = max(result)[0]
result = set(cell.pop() for r, cell in result if 1 < r == maxradius)
return result
if __name__ == '__main__':
assert landing_site({'E5', 'E7', 'F4', 'F6', 'G4', 'G6', 'H3', 'H5'}) == {'C3', 'J7'}, 'crevasse'
assert landing_site({'A4', 'C2', 'C6', 'C9', 'D4', 'D7', 'F1', 'F5',
'F8', 'G4', 'H7', 'I2', 'I5', 'I9', 'K3', 'K8', 'L5'}) == {'B7', 'E3', 'J6'}, 'stones'
assert landing_site({'D3', 'D4', 'D5', 'D6', 'E3', 'E7', 'F2', 'F7', 'G2',
'G8', 'H2', 'H7', 'I3', 'I7', 'J3', 'J4', 'J5', 'J6'}) == {'G5'}, 'crater'
assert landing_site(set()) == {'E5', 'F5', 'G5', 'H5'}, 'plane'
assert landing_site({chr(c+65)+str(r+1) for c in range(12) for r in range(9)}) == set(), 'wasteland'
print('The local tests are done. Click on "Check" for more real tests.')
Sept. 11, 2022