fromcollectionsimportdefaultdictfromcollectionsimportCounterfromitertoolsimportproductimportnumpyasnpfromscipy.spatialimportKDTreewithopen("input.txt")asf:coords=[(int(x.split(",")[0]),int(x.split(",")[1]))forxinf]xs,ys=[xforx,yincoords],[yforx,yincoords]# Part 1
t=KDTree(coords)d=defaultdict(int)edge=set()fori,jinproduct(range(max(xs)),range(max(ys))):points,dists=t.query((i,j),k=2,p=1)ifi==0orj==0ori==max(xs)-1orj==max(ys)-1:edge.add(int(dists[0]))ifpoints[0]!=points[1]:d[(i,j)]=dists[0]forp,areainCounter(d.values()).most_common():ifpnotinedge:print(area)break# Part 2
defdist(x,y):returnsum(abs(x-a)+abs(y-b)fora,bincoords)print(sum(1fori,jinproduct(range(max(xs)),range(max(ys)))ifdist(i,j)<10000))
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Python using a kd-tree.