Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Calc neighb solution in Clear category for Landing Site by U.V
from typing import Set
from itertools import product
C = 'ABCDEFGHIJKL'
R = '123456789'
def a2d(a):
return R.find(a[1]), C.find(a[0])
def d2a(p):
return C[p[1]] + R[p[0]]
# neighbors of 'a' without obstacles
def nbObs(a, obstacles): # a - decimal !
r, c = a
p = -((c +1) % 2)
return [(r1, c1) for r1, c1 in [(r-1, c), (r+1, c), (r+p, c-1), (r+p, c+1), (r+1+p, c+1), (r+p+1, c-1)]
if 0<= r1<9 and 0<=c1<12 and((r1,c1) not in obstacles)]
def area(c, obsDec):
return len (ring(c, obsDec))
def ring(rc, obsDec):
rings = [{rc}]
while 1:
nbs = set()
rr = rings[-1].copy()
for c in rr:
n = nbObs(c, obsDec)
if not n or len(n) % 6:
return rings
nbs.update(n)
rings.append(nbs)
area = {}
for m in rings:
area.update(m)
def landing_site(obstacles: Set[str]) -> Set[str]:
obsDec = set(a2d(rc) for rc in obstacles)
dstnc = {}
for rc in product(range(1, 8), range(1, 11)):
if rc in obsDec:
continue
dstnc[d2a(rc)] = area(rc, obsDec)
if not dstnc:
return set()
m = max(dstnc.values())
print('Max R:', m)
if m < 2:
return []
return set([k for k, v in dstnc.items() if v==m])
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.')
Dec. 31, 2022