const{readFile}=require('./reader');const{buildMap,buildPath}=require('./13-common');constgetSquareCrash=carts=>{while(true){carts.sort((a,b)=>{constsA=a.square;constsB=b.square;return(sA.x===sB.x)?sA.y-sB.y:sA.x-sB.x;});for(letcartofcarts){cart.move();if(cart.crashed){returncart.square;}}};};(async()=>{constlines=awaitreadFile('13-input.txt');constmap=buildMap(lines);const{squares,carts}=buildPath(map);constsquare=getSquareCrash(carts);console.log(`The location of the first crash is ${square.y},${square.x}`);})();
13b.js
const{readFile}=require('./reader');const{buildMap,buildPath}=require('./13-common');constgetRemainingCart=carts=>{lettick=0;while(carts.length>1){carts.sort((a,b)=>{constsA=a.square;constsB=b.square;return(sA.x===sB.x)?sA.y-sB.y:sA.x-sB.x;});for(letcartofcarts){if(!cart.crashed){cart.move();if(cart.crashed){cart.square.cart=null;}}}carts=carts.filter(cart=>!cart.crashed);tick++};returncarts[0];};(async()=>{constlines=awaitreadFile('13-input.txt');constmap=buildMap(lines);const{squares,carts}=buildPath(map);constcart=getRemainingCart(carts);console.log(`The location of the last cart ${cart.id} is ${cart.square.y},${cart.square.x}`);})();
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.
JavaScript solution
I had to do other things since Wednesday and couldn't focus on AoC until yesterday night but hopefully I will catch up soon!
I created a graph for all the map squares, and a list of carts that move around these squares.
Here's my solutions:
13-common.js
13a.js
13b.js