--- Day 16: Packet Management ---

December 16

Listening to: Vaporwave


Today's puzzle is such a long read.... so... so long. It's giving me actual flashbacks to college, in my courses on networking, the OSI model, etc. I honestly think that the next few days puzzles will be building out something to implement this protocol whatever it is.

Part one is just asking for a very small number from the packets in general, but I have a feeling that it's going to be a multi-day puzzle now, ending in full decoding and executing of some program based on the input.

I'm half convinced to get my network-engineer friends to do this day's puzzle for me.

~

Okay, enough grumbling. I'm thinking the best way to deal with this might actually be to convert this to a stream of some kind? Oh wait, no. I guess a vector<bool> is handled by the compiler as something special, but with the standard vector interface. Neat? Or maybe I'll keep it as a string for simplicity? Or something?

int takeBits(string &message, int length) {
    int val = 0;
    for (int i = 0; i < length; i++) {
        val <<= 1;
        val += message[i] - '0';
    }

    message = message.substr(length);
    return val;
}

This turned out to be useful. Actively consuming bits as I read them. I'm using it everywhere in my nested switch statements. Whee.

int parsePacket(string &message) {
    int version = takeBits(message, 3);
    int type = takeBits(message, 3);

    int totalSum = version;

    switch (type) {
        case 4: {
            // Do nothing with it now, lol
            int literal = takeLiteral(message);
            break;
        }

        default:
            int lengthId = takeBits(message, 1);
            int packetLength;
            switch(lengthId) {
                case 0: {
                    // Awful case-specific handling
                }
                case 1: {
                    // Even-more awful case-specific handling
                }
            }

            break;
    }

    return totalSum;
}

Seems to work! It didn't work all at once, I had to do a bit of tweaking on each case, but nothing particularly bad. Enjoyable so far.


Oh. Part two is "Draw the rest of the f#$%ing owl."

At least it's well defined, and it's basically "here's the different things to do with the operators", which doesn't seem too difficult. Collect the packet values, then do the operation on them.

    uint64_t ret = 0;
    switch (type) {
        case 0: // Sum
            for (auto n: values) ret += n;
            return ret;

        case 1: // Product
            // Repeat similar logic for a bunch of cases
    }

And it worked first time.

Except I didn't know it because I had a bug in my printf() statement.

~

Thank you Demon for all of the reluctant help and moral support for this part!


My solutions for today's puzzles