After spending a good night sleep on the train, I guess it is finally time to wake up for my first stop. Apparently this stop is some kind of farm. I wonder what kind of things they grow here…
Suddenly, I start to hear loud noises from a distance. I get up and try to understand what’s going on. The noise seems to be coming from the crates that are being loaded onto the train. An elf explains to me that they contain thunder.
“Ah, I see… Obviously… Wait what? Thunder? As in thunder like the noise made by a lightning?”, I said, surprised.
- “Yup!”, the elf answered, carrying on with their loading of the crate, as if that was just a regular wednesday to them.
These crates are too noisy, and I hate noise, so I walk away and explore the farm in search of anything nice to distract myself with.
Thanskfully, it didn’t take long before finding some elves in need of my expert (editor’s note: not an expert at all) Haskell skills!
The elves are facing a simple problem: they have a robot which follows an infinite series of waypoints “roygbv” (repeat this forever, because obviously this farm is infinite. Or it is a loop, your guess.)
The robot follows a simple program: it goes to the next waypoint in its program.
So for example, if the program is “obbvb”, the robot starts at the first r, then goes to the first o, then the first b, then the second b, then the second v (we skipped the first v when going to the second b) and finally the third b.
The task is simple: we want to find the total distance traveled.
The idea lies in a simple concept: Modular Arithmetic (Believe me, maths are fun!)
We can represent each waypoint as a number: o is 0, y is 1 … v is 4 and r is 5 (why did I choose r as 5 instead of 0? It’s a surprise tool for later ;) And it doesn’t matter here, the point of modular arithmetic is that it loops back every n steps, here n being 6. It doesn’t matter if the position we start at is 0 or 5, as long as we’re consistant).
parseInput :: String -> Input
parseInput = map (`elemIndexJust` "oygbvr")
The distance from o to y is simple, it’s just the usual way to compute a distance: 1 - 0 Same thing with o to g, o to b, o to r, y to g, y to b, y to r etc.
But what about going from r to o? 0 - 5 = -5! We can’t have a negative distance!
This is where modular arithmetic comes into play. Basically, -5 is the same as 1 mod 6. A way to see this is saying that going five steps backwards is the same as going one step forward on a 6-steps loop.
That being done, we still need to handle the dreaded case of r to r. 0 - 0 = 0 which is 0 mod 6. That’s embarassing, because what we want here is 6… What about adding 6 before taking the modulo? Well, 0 - 0 + 6 = 6 which is… 0 mod 6 :(
The trick here is to shift everything by -1 before the modulo, then add 1 again.
For all other cases this doesn’t change anything: n mod 6 + 1 = (n + 1) mod 6 for -6 <= n <= 4. That is to say, if there is a distance between 1 and 5, the result is the same. In the case where start == end, then we have 0 - 1 mod 6 + 1 = 5 + 1 = 6, which is the result we want.
So, we can just compute the distances between each two waypoints in the program, starting at an ‘r’ (which is represented as 5 here), and sum them:
partOne :: Input -> Output
partOne input = sum . map (\(s, e) -> (e - s - 1) `mod` 6 + 1) $ travels
where
travels = zip (5 : input) input
Of course this isn’t enough, and the elves want the robot to travel a bigger distance.
In order to do that, they want to rearrange the program so that it always forces the robot to move as far as possible.
The biggest step possible is always to go to the same waypoint, so equal waypoints should be next to each other.
Then, if there are no more of the same waypoint, the next one should be the furthest away, which is the biggest waypoint going backwards…
In other words: we need to sort this program from the biggest waypoint to the smallest (refering to their number representation).
“But wait!”, I hear you say (although you’re just an hypothetical voice in my head that I get while writing this), “I used 0 to represent ‘r’, as it felt the most logical; after all we start at an ‘r’. However, sorting in decreasing order would put r last, but we want to do them first”
Well yes, which is why representing them as 5 is the way to go: it doesn’t change the distance computation, and it encodes the idea that r is the furthest step we can take t the beggining! :)
partTwo :: Input -> Output
partTwo = partOne . sortBy (comparing Down)
This was fun, but I’ve just realized something: I had so much fun with the elves that it was now 1AM and I was starting to feel exhausted.
Writing this adventure log is fun, but I should go back to my cabin and sleep. After all, I’ve got a hunch that some elves will need my help tomorrow…