Took so much time for part two. Actually went ahead and rewrote part one as well. Solution in swift
structPoint:CustomStringConvertible,Equatable,Hashable{varx:Intvary:Intvardescription:String{get{return"X: \(self.x) Y: \(self.y)"}}}vargrid:[Point]=[]_=input.enumerated().map{(y,layer)in_=layer.enumerated().map{(x,point)inifpoint=="#"{grid.append(Point.init(x:x,y:y))}}}funcangleBtw(_x:Point,_y:Point)->Double{return(atan2(Double(x.y-y.y),Double(x.x-y.x))*180)/Double.pi}structLos:CustomStringConvertible{varpoint:Pointvarangle:Doublevardescription:String{get{return"point: \(self.point) angle: \(self.angle)"}}}funcgetAstroidsInLOS(_base:Point)->[Los]{varlos:[Los]=[]letastroids=grid.filter{$0!=base}_=astroids.map{pointinletangle=angleBtw(base,point)varblocked=false_=los.map{aLosinifaLos.angle==angle{blocked=true}}if!blocked{los.append(Los.init(point:point,angle:angle))}}returnlos}funcpartOne(){letresult=grid.map{point->IntinreturngetAstroidsInLOS(point).count}.max()print("Part One answer is :\(result??0)")}funcpartTwo(){letroot=grid.map{point->(Point,Int)inreturn(point,getAstroidsInLOS(point).count)}.max{(p1,p2)->Boolinreturnp1.1<p2.1}!.0varlos=getAstroidsInLOS(root)los.sort{a,bina.angle<b.angle}letbase=los.firstIndex{(aLos)->BoolinreturnaLos.angle==90}letresult=los[199+(base??0)-los.count]print("Part Two is \(result.point.x*100+result.point.y)")}partOne()partTwo()
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.
Took so much time for part two. Actually went ahead and rewrote part one as well. Solution in swift