AoC 2016/11 - New identifier

TL;DR

On with Advent of Code puzzle 11 from 2016: a new identifier sub for the new representation.

Now that we have a New representation and a way to parse it, it’s time to start adapting the machinery needed by the solving algorithm.

We will start simple, i.e. the identifier function. This has to be a string that can represent a state in a one-to-one mapping, so that equivalent states (i.e. states where the elevator, the generators and the microchips are all in the same place) map onto the same identifier, and different states (for either the elevator, the generators or the microchips) map onto different ones.

The need for a string is that it will eventually be used as a key in a hash… so it’s better that it stringifies well!

One alternative might be to just join these three components together:

sub id_of ($state) {
   return join '-', $state->@{qw<elevator generators microchips>};
}

To keep stuff as tight as possible, though, we can rely on pack:

sub id_of ($state) {
   return pack 'AN2', $state->@{qw<elevator generators microchips>};
}

In this case:

  • the elevator is encoded with a single octet, that is actually the same as the character representing the corresponding digit;
  • both the generators and the microchips are represented as a 32-bit sequence, in network order. The order here is not important, as long as every state is encoded in the same way.

This yields a total of 9 octets for each identifier, yay!


Comments? Octodon, , GitHub, Reddit, or drop me a line!