The worst for this day was understanding the instructions.
I just couldn't for the life of me understand what was being asked, until a friend helped me and then I was on my way.
Full disclosure: I was feeling pretty dumb, and almost gave up. I'm glad I didn't 🙏
importFsfrom"fs"importPathfrom"path"constinput=Fs.readFileSync(Path.join(__dirname,"input.txt")).toString().trim().split("\n")interfaceCoordinates{x:numbery:number}typeDistance=numbertypeBoundaries=[Coordinates,Coordinates]typeID=stringtypePixel=null|IDfunctiongetIdFromCoordinates(coords:Coordinates):ID{return`${coords.x}${coords.y}`}functionconvertToCoordinates(line:string):Coordinates{const[x,y]=line.split(", ")return{x:parseInt(x,10),y:parseInt(y,10)}}functioncomputeManhattanDistance(a:Coordinates,b:Coordinates):Distance{returnMath.abs(a.x-b.x)+Math.abs(a.y-b.y)}functioncomputeBoardBoundaries(coords:Array<Coordinates>):Boundaries{returncoords.reduce((boundaries:Boundaries,coordinate:Coordinates,index:number)=>{let[topLeftBoundary,bottomRightBoundary]=boundariesif(index===0){return[{...coordinate},{...coordinate}]asBoundaries}if(coordinate.x<topLeftBoundary.x){topLeftBoundary.x=coordinate.x}if(coordinate.y<topLeftBoundary.y){topLeftBoundary.y=coordinate.y}if(coordinate.x>bottomRightBoundary.x){bottomRightBoundary.x=coordinate.x}if(coordinate.y>bottomRightBoundary.y){bottomRightBoundary.y=coordinate.y}return[topLeftBoundary,bottomRightBoundary]asBoundaries},[// first element is top left boundary{x:0,y:0},// last element is bottom right boundary{x:0,y:0},]asBoundaries)}functiondoesBeaconHaveFiniteArea(beacon:Coordinates,beacons:Array<Coordinates>):Boolean{lethasTopLeft=falselethasTopRight=falselethasBottomLeft=falselethasBottomRight=falsefor(leti=0;i<beacons.length;i++){constcomparedBeacon=beacons[i]if(hasTopRight===false&&comparedBeacon.x>=beacon.x&&comparedBeacon.y>beacon.y){hasTopRight=true}if(hasBottomRight===false&&comparedBeacon.x>=beacon.x&&comparedBeacon.y<beacon.y){hasBottomRight=true}if(hasBottomLeft===false&&comparedBeacon.x<=beacon.x&&comparedBeacon.y<beacon.y){hasBottomLeft=true}if(hasTopLeft===false&&comparedBeacon.x<=beacon.x&&comparedBeacon.y>beacon.y){hasTopLeft=true}}returnhasTopLeft&&hasTopRight&&hasBottomLeft&&hasBottomRight}constcoordinates=input.map(convertToCoordinates)constbeaconsWithFiniteArea:Array<Coordinates>=coordinates.reduce((beacons,beacon)=>{consthasFiniteArea=doesBeaconHaveFiniteArea(beacon,coordinates.filter(ref=>ref.x!==beacon.x||ref.y!==beacon.y))if(hasFiniteArea){return[...beacons,beacon]}returnbeacons},[]asArray<Coordinates>)constboundaries:Boundaries=computeBoardBoundaries(coordinates)console.log(JSON.stringify(boundaries))functioncomputePixelForCoords(coordinates:Coordinates,coordinatesList:Array<Coordinates>):Pixel{constdistances=coordinatesList.map(beacon=>{return{id:getIdFromCoordinates(beacon),distance:computeManhattanDistance(coordinates,beacon),}}).sort((a,b)=>a.distance-b.distance)if(distances[0].distance===distances[1].distance){returnnull}returndistances[0].id}constcanvas:Array<Array<Pixel>>=[]for(letx=boundaries[0].x;x<=boundaries[1].x;x++){for(lety=boundaries[0].y;y<=boundaries[1].y;y++){if(canvas[x]===undefined){canvas[x]=[]}canvas[x][y]=computePixelForCoords({x,y},coordinates)}}constsorted=beaconsWithFiniteArea.map(beacon=>{letcount=0letid=getIdFromCoordinates(beacon)for(letx=boundaries[0].x;x<=boundaries[1].x;x++){for(lety=boundaries[0].y;y<=boundaries[1].y;y++){if(canvas[x][y]===id){count++}}}returncount}).sort((a,b)=>b-a)// 01console.log(sorted[0])
Part2:
importFsfrom"fs"importPathfrom"path"constinput=Fs.readFileSync(Path.join(__dirname,"input.txt")).toString().trim().split("\n")interfaceCoordinates{x:numbery:number}typeDistance=numbertypeBoundaries=[Coordinates,Coordinates]functionconvertToCoordinates(line:string):Coordinates{const[x,y]=line.split(", ")return{x:parseInt(x,10),y:parseInt(y,10)}}functioncomputeManhattanDistance(a:Coordinates,b:Coordinates):Distance{returnMath.abs(a.x-b.x)+Math.abs(a.y-b.y)}functioncomputeBoardBoundaries(coords:Array<Coordinates>):Boundaries{returncoords.reduce((boundaries:Boundaries,coordinate:Coordinates,index:number)=>{let[topLeftBoundary,bottomRightBoundary]=boundariesif(index===0){return[{...coordinate},{...coordinate}]asBoundaries}if(coordinate.x<topLeftBoundary.x){topLeftBoundary.x=coordinate.x}if(coordinate.y<topLeftBoundary.y){topLeftBoundary.y=coordinate.y}if(coordinate.x>bottomRightBoundary.x){bottomRightBoundary.x=coordinate.x}if(coordinate.y>bottomRightBoundary.y){bottomRightBoundary.y=coordinate.y}return[topLeftBoundary,bottomRightBoundary]asBoundaries},[// first element is top left boundary{x:0,y:0},// last element is bottom right boundary{x:0,y:0},]asBoundaries)}constcoordinates=input.map(convertToCoordinates)constboundaries:Boundaries=computeBoardBoundaries(coordinates)constMAX_DISTANCE=10000letclosestSum:number=-Infinityletposition:Coordinates={x:Infinity,y:Infinity}letnumberOfPixelsForRegion:number=0for(letx=boundaries[0].x;x<=boundaries[1].x;x++){for(lety=boundaries[0].y;y<=boundaries[1].y;y++){constbeacon={x,y}constsum=coordinates.reduce((total,point)=>{returntotal+computeManhattanDistance(beacon,point)},0)if(sum<MAX_DISTANCE){numberOfPixelsForRegion++if(sum>closestSum){closestSum=sumposition={x,y}}}}}console.log(`Chosen position ${JSON.stringify(position)} with sum ${closestSum} and region size: ${numberOfPixelsForRegion}`)
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.
The worst for this day was understanding the instructions.
I just couldn't for the life of me understand what was being asked, until a friend helped me and then I was on my way.
Full disclosure: I was feeling pretty dumb, and almost gave up. I'm glad I didn't 🙏
Part2: