I actually had the right idea right off the bat, but I tried to cut the corners thinking if would have been too cumbersome... It turned out it was the opposite! 😄
But... Let's start with
Part One
This was fairly simple. All we had to do is to write a function that takes a step and evolves it into the next one. I used a JavaScript generator again, because why the heck not:
constcoordRE=/<x=(-?\d+), y=(-?\d+), z=(-?\d+)>/;function*getStates(){// Every satellite state is a vector of six numbersletsatellites=input.trim().split('\n').map(line=>([...line.match(coordRE).slice(1).map(Number),0,0,0]));while(true){satellites=satellites.map(satellite=>{constvelocity=satellite.slice(3);constnextVelocity=velocity.map((value,index)=>{returnsatellites.reduce((diff,sat)=>{returndiff+Math.sign(sat[index]-satellite[index])},value);});constnextPosition=satellite.slice(0,3).map((value,index)=>value+nextVelocity[index]);return[...nextPosition,...nextVelocity];});yieldsatellites;}}functiongetTaxiDriverLength(vector){returnvector.reduce((sum,length)=>sum+Math.abs(length),0);}functiongetTotalEnergy(satellites){returnsatellites.reduce((total,satellite)=>{returntotal+getTaxiDriverLength(satellite.slice(0,3))*getTaxiDriverLength(satellite.slice(3));},0)}conststates=getStates();letsatellites;for(leti=0;i<1000;i++){({value:satellites}=states.next());}console.log(getTotalEnergy(satellites));
I don't think there's much to say here...
Part Two
Now it comes the tricky part... My solution is 332477126821644 (~3.3e14), yours will be similar in size, so brute force is out of question.
I thought that this system is quite chaotic, but not that much chaotic, so satelite configurations could have a shorter period. When all 4 periods are found, all I had to do was to compute the least common multiple and I would have been golden! Right?
Well, in theory. In practice, I couldn't find the period of any of my satellites after one billion cycles, so that was no good. The next attempt consisted in splitting a satellite's state into position and velocity vectors, hoping that would have reduced their periods enough. And it did, but... that led me to a very high result.
Note that a "period" at that time consisted in the step number of the first time a vector was equal to the respective initial value. That's wrong too. For instance, in the first given example (with a period of 2772), the position of Europa (the second satellite) was the same of the initial position after 616, 1232, 1539, 2155, 2771 and 2772 steps. No clear period was evident.
But computing the differences between step marks gives the following sequence: 616, 616, 307, 616, 616, 1, 616, 616, 307, 616, 616, 1, 616, 616, 307, 616, 616, 1, 616... and so on. Grouping these numbers every 6 gives us a period of exactly 2772 steps. So I made this function to compute the period of a given array of (distances between) marks:
functiongetPeriod(distances){for(letlength=1;length<=distances.length;length++){constsums=[];constlimit=distances.length-distances.length%length;for(leti=0;i<limit;i+=length){letsum=0;for(letj=0;j<length;j++){sum+=distances[i+j];}sums.push(sum);}// If every sum is the same, then that sum is *probably* the periodif(sums.length>0&&sums.every(sum=>sum===sums[0])){returnsums[0];}}}
The following correction was to split everything down to every single vector component, and started marking all the occurences of a value equalling the initial value. And lo and behold, periods started to come out after "just" one million iterations! This is the last part of my script:
I admit that I actually did not compute the least common multiple. That problem wasn't interesting and I just used an online calculator to get the final result 😂
As usual, text, complete solutions and input are on my repo.
Tomorrow... IntCodes?
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.
Finally got the second part! 🥳
I actually had the right idea right off the bat, but I tried to cut the corners thinking if would have been too cumbersome... It turned out it was the opposite! 😄
But... Let's start with
Part One
This was fairly simple. All we had to do is to write a function that takes a step and evolves it into the next one. I used a JavaScript generator again, because why the heck not:
I don't think there's much to say here...
Part Two
Now it comes the tricky part... My solution is 332477126821644 (~3.3e14), yours will be similar in size, so brute force is out of question.
I thought that this system is quite chaotic, but not that much chaotic, so satelite configurations could have a shorter period. When all 4 periods are found, all I had to do was to compute the least common multiple and I would have been golden! Right?
Well, in theory. In practice, I couldn't find the period of any of my satellites after one billion cycles, so that was no good. The next attempt consisted in splitting a satellite's state into position and velocity vectors, hoping that would have reduced their periods enough. And it did, but... that led me to a very high result.
Note that a "period" at that time consisted in the step number of the first time a vector was equal to the respective initial value. That's wrong too. For instance, in the first given example (with a period of 2772), the position of Europa (the second satellite) was the same of the initial position after 616, 1232, 1539, 2155, 2771 and 2772 steps. No clear period was evident.
But computing the differences between step marks gives the following sequence: 616, 616, 307, 616, 616, 1, 616, 616, 307, 616, 616, 1, 616, 616, 307, 616, 616, 1, 616... and so on. Grouping these numbers every 6 gives us a period of exactly 2772 steps. So I made this function to compute the period of a given array of (distances between) marks:
The following correction was to split everything down to every single vector component, and started marking all the occurences of a value equalling the initial value. And lo and behold, periods started to come out after "just" one million iterations! This is the last part of my script:
I admit that I actually did not compute the least common multiple. That problem wasn't interesting and I just used an online calculator to get the final result 😂
As usual, text, complete solutions and input are on my repo.
Tomorrow... IntCodes?