Bit late to the party on this one. With good reason, but also I kept making mistakes with how to add up the jumps.
Got there eventually.
Part 2 first:
functionorbitsToSanta(){letpretext=document.getElementsByTagName('pre')[0].innerHTML;letorbits=pretext.split('\n').filter(o=>o!=='');letorbithash={};letmeorbit='';letsanorbit='';for(letorbitoforbits){orbithash[orbit]=newleaf(orbit);}for(letorbitoforbits){let[main,sub]=orbit.split(')');if(sub==='YOU'){meorbit=orbit;}if(sub==='SAN'){sanorbit=orbit;}letparent=orbits.filter(oo=>{let[othermain,othersub]=oo.split(')');returnmain===othersub;})[0];if(typeof(parent)!=='undefined')//root node has no parent. {orbithash[orbit].parent=orbithash[parent];}}letmelist=orbithash[meorbit].listOrbits();letsanlist=orbithash[sanorbit].listOrbits();// hco is Highest Common Orbitlethco=melist.filter(o=>sanlist.indexOf(o)>-1).slice(-1)[0];lethcolist=orbithash[hco].listOrbits();letmelength=melist.filter(l=>hcolist.indexOf(l)===-1).length-1;// minus 1 because you exclude the current orbit.letsanlength=sanlist.filter(l=>hcolist.indexOf(l)===-1).length-1;// minus 1 because the hco is already counted.returnmelength+sanlength;}functionleaf(orbit){this.orbit=orbit;this.parent=null;}leaf.prototype.orbitCounts=function(){letcount=1;if(this.parent!==null){count=1+this.parent.orbitCounts();}returncount;};leaf.prototype.listOrbits=function(){if(this.parent===null){return[];}letpos=this.parent.listOrbits();pos.push(this.orbit);returnpos;};
Part 1 had the following code in place of the number of jumps calculation:
Bit late to the party on this one. With good reason, but also I kept making mistakes with how to add up the jumps.
Got there eventually.
Part 2 first:
Part 1 had the following code in place of the number of jumps calculation: