Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Bats Bunker by coells
import math
# raytracing on map abusing low input precision and rounding on stochastic positions
def iseeyou(a, b, map):
ishit = lambda x, y: map[int(round(y))][int(round(x))] == 'W';
dx, dy = (b[0] - a[0]) / 16.0, (b[1] - a[1]) / 16.0
ex, ey = dx / 4.0, dy / 4.0
x0, y0 = a[0], a[1]
for i in range(16):
x, y = x0 + i * dx, y0 + i * dy
sx1, sy1 = x - ex, y - ey
sx2, sy2 = x + ex, y - ey
sx3, sy3 = x + ex, y + ey
sx4, sy4 = x - ex, y + ey
if ishit(sx1, sy1) or ishit(sx2, sy2) or ishit(sx3, sy3) or ishit(sx4, sy4):
return False
return True
# simple wave-spreading search
def checkio(map):
bats = [[(x, y), map[y][x], 0.0] for y in range(len(map)) for x in range(len(map[y])) if 'AB'.count(map[y][x])]
alpha = [x for x in bats if x[1] == 'A'][0]
bats[0][2] = 1.0
while not alpha[2]:
for start in [x for x in bats if x[2]]:
for end in bats:
dist = start[2] + math.sqrt((start[0][0] - end[0][0]) ** 2 + (start[0][1] - end[0][1]) ** 2)
if (not end[2] or dist < end[2]) and iseeyou(start[0], end[0], map):
end[2] = dist
return round(alpha[2] - 1.0, 2)
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
def almost_equal(checked, correct, significant_digits=2):
precision = 0.1 ** significant_digits
return correct - precision < checked < correct + precision
assert almost_equal(checkio([
"B--",
"---",
"--A"]), 2.83), "1st example"
assert almost_equal(checkio([
"B-B",
"BW-",
"-BA"]), 4), "2nd example"
assert almost_equal(checkio([
"BWB--B",
"-W-WW-",
"B-BWAB"]), 12), "3rd example"
assert almost_equal(checkio([
"B---B-",
"-WWW-B",
"-WA--B",
"-W-B--",
"-WWW-B",
"B-BWB-"]), 9.24), "4th example"
March 28, 2014
Comments: