Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in 3rd party category for Searchlights by sawako.oono
import numpy as np
import math
from itertools import chain
def searchlights(polygons, lights):
def vertices(poly):
verts=[(poly[0],poly[1])]
l = poly[2]
vno=poly[3]
r = l/(2*np.sin(np.pi/vno))
center=(verts[0][0],verts[0][1]-r)
for i in range(1,vno):
x=r*np.sin(2*np.pi*i / vno)+center[0]
y=r*np.cos(2*np.pi*i / vno)+center[1]
verts.append((x,y))
return verts
def in_the_light(light,pos):
center=(light[0],light[1])
r=light[2]
return (math.sqrt((pos[0]-center[0])**2+(pos[1]-center[1])**2)<=r)
verts=list(chain(*[vertices(poly) for poly in polygons]))
visible=set()
for light in lights:
for v in verts:
if in_the_light(light,v) and v[0]>=0 and v[1]>=0:
visible.add(v)
return len(visible)
if __name__ == '__main__':
print("Example:")
print (searchlights([(2, 3, 2, 3)], [(1, 2, 1)]))
# These "asserts" are used for self-checking and not for an auto-testing
assert(searchlights([(2, 3, 2, 3)], [(1, 2, 1)])) == 1, 'regular triangle'
assert(searchlights([(4, 5, 2, 4)], [(4, 4, 3)])) == 4, 'square'
assert(searchlights([(6, 7, 2, 5)], [(2, 3, 2)])) == 0, 'regular pentagon'
assert(searchlights([(4, 2, 2, 6)], [(4, 2, 3)])) == 3, 'regular hexagon'
assert(searchlights([(1, 7, 2, 8)], [(0, 5, 4)])) == 5, 'regular octagon'
print("Coding complete? Click 'Check' to earn cool rewards!")
June 19, 2021
Comments: