After about 20 hours (distributed over 4 days) I finally got this one! I didn't give up, it felt like a real-life project, requiring research and intensive debugging, and after so much effort, it was SOOO satisfying to see it running.!
For me, Part 1 executed in 3 minutes, and Part 2 took 54 minutes!!!
I would be more than happy to answer questions and give tips about that, but basically I really thank @neilgall for his fantastic explanation and the usage of Dijkstra's algorithm.
I'm gonna omit reader.js which is the same as the other solutions and jump to the point:
const{readFile}=require('./reader');const{readDungeon,makeRound,getGoblins,getElves,printStats,getOutcome}=require('./15-common');(async ()=>{constlines=awaitreadFile('15-input.txt');const{dungeon,units}=readDungeon(lines);letgoblins,elves,rounds=0;do{consthasCombatEndedEarly=makeRound(dungeon,units);if (!hasCombatEndedEarly)rounds++;goblins=getGoblins(units).length;elves=getElves(units).length;printStats(rounds,dungeon,units);}while (goblins>0&&elves>0);console.log(`The ${goblins>0?'goblins':'elves'} won!`);console.log(`The outcome of the combat is ${getOutcome(rounds,units)}`);})();
15b.js
const{readFile}=require('./reader');const{readDungeon,makeRound,getElves,getGoblins,printStats,getOutcome}=require('./15-common');(async ()=>{constlines=awaitreadFile('15-input.txt');letap=3;letareAllElvesAlive;do{ap++;console.log(`\nWith AP as ${ap}:`);const{dungeon,units}=readDungeon(lines);constelves=getElves(units);elves.forEach(elf=>elf.ap=ap);letinitialElvesCount=elves.length;letelvesCount,goblinsCount;letrounds=0;do{console.log(`AP ${ap}, round ${rounds+1}`);consthasCombatEndedEarly=makeRound(dungeon,units);if (!hasCombatEndedEarly)rounds++;goblinsCount=getGoblins(units).length;elvesCount=getElves(units).length;areAllElvesAlive=elvesCount===initialElvesCount;}while (areAllElvesAlive&&goblinsCount>0);printStats(rounds,dungeon,units);if (areAllElvesAlive){console.log(`\nAll elves survived when AP is ${ap}`);console.log(`The outcome of the last combat is ${getOutcome(rounds,units)}`);}else{console.log(`There was an elf casualty!`);}}while (!areAllElvesAlive);})();
Ryan is an engineer in the Sacramento Area with a focus in Python, Ruby, and Rust. Bash/Python Exercism mentor. Coding, physics, calculus, music, woodworking. Looking for work!
Is your path finding algorithm the dijkstra one? Thanks, it was very clear how it works, maybe because it in javascript :), I used it for my solution for part one.
But I didn't understand how you choose the next step exactly. It works, but I can't see where you break the ties in reading order. Anyhow, nicely done, learned how to find shortest route with your code.
Hi @askeroff
, thanks!! Yes, I'm using Dijkstra's algorithm (even though I didn't explicitly mention it in the code), but it's implemented on getMinimumDistance function, where I basically do the following steps to get the minimum step between "start" and "end" squares:
create a map for the distances of all squares and set each one of them as Number.POSITIVE_INFINITY
create a set to mark unvisited squares and add every square to it
set "current" as the unvisited square with the lowest distance (or the "start" square in the beginning)
for each unvisited unblocked neighbors of "current", update the distance to the minimum between distance(current)+1 and the neighbor's current distance.
mark "current" as visited (by removing it from the unvisited squares set)
go back to step 3 and repeat until there are no more unvisited squares
The final distance between "start" and "end" is the smallest distance of all "end"'s unblocked neighbors.
Also, to break the ties in the reading order, it's all in getAdjacents function, where I look for the following neighbor squares and return in their reading order.
Considering we're getting adjacents for position X,Y, N=max(X) and M=max(Y)
JavaScript solution
After about 20 hours (distributed over 4 days) I finally got this one! I didn't give up, it felt like a real-life project, requiring research and intensive debugging, and after so much effort, it was SOOO satisfying to see it running.!
For me, Part 1 executed in 3 minutes, and Part 2 took 54 minutes!!!
I would be more than happy to answer questions and give tips about that, but basically I really thank @neilgall for his fantastic explanation and the usage of Dijkstra's algorithm.
I'm gonna omit reader.js which is the same as the other solutions and jump to the point:
15-common.js
15a.js
15b.js
Awesome! Really nice work!
Is your path finding algorithm the dijkstra one? Thanks, it was very clear how it works, maybe because it in javascript :), I used it for my solution for part one.
But I didn't understand how you choose the next step exactly. It works, but I can't see where you break the ties in reading order. Anyhow, nicely done, learned how to find shortest route with your code.
Hi @askeroff , thanks!! Yes, I'm using Dijkstra's algorithm (even though I didn't explicitly mention it in the code), but it's implemented on
getMinimumDistance
function, where I basically do the following steps to get the minimum step between "start" and "end" squares:Number.POSITIVE_INFINITY
The final distance between "start" and "end" is the smallest distance of all "end"'s unblocked neighbors.
Also, to break the ties in the reading order, it's all in
getAdjacents
function, where I look for the following neighbor squares and return in their reading order.Considering we're getting adjacents for position X,Y, N=max(X) and M=max(Y)
In other words,
Oh, awesome. I got it. I want to come back after I've done others and revisit this problem with breadth-first-search. Maybe it'll be faster.