Module advent_of_code_2021::day_16 [−][src]
Expand description
This is my solution for Advent of Code - Day 16 - Packet Decoder
After yesterday’s very technical/theory heavy solution today was working through a fairly complex specification, but the face-value implementation was efficient once all the parts were in place.
The bulk of the work is in parsing the input into the hierarchy of packets, parse_input.
This is the entry point for a number of functions that are involved in the parsing process.
to_bits is a bit clunky, but returns the bits as a Vec<bool> in reverse order so that the
bits can be consumed with Vec::pop which is much more efficient than taking them from the
head of the Vec. take_bits consumes a specified number of bits from the tail, interpreting
them as a number. parse_packet consumes the version and PacketType, then delegates to
parse_literal and parse_sub_packets based on the type. Each uses take_bits as
appropriate to consume and interpret the required bits according to the spec, and keeps track of
bits consumed to report back to any parent operation packet that is reading in bit length mode.
Once that was done both part one Packet::version_sum, and part two Packet::compute
recursively walk the packet tree compiling the appropriate solution.
Structs
Represents a packet in BITS
Enums
The eight possible packet types
Functions
Parse the section of a literal packet representing the number. This will be in chunks of 5 bits,
the first being a flag that indicates if parsing should continue after this chunk, the next four
being the next four bits in the number. Once the continue flag is 0 indicating this is the
final chunk, all four-bit sections should be concatenated and interpreted as the binary
representation of a usize. Returns the value and number of bits consumed.
Read the packet header (version: 3 bits, type: 3 bits). Then based of the type delegate the
parsing of the payload to either parse_literal or parse_sub_packets. Return the parsed
Packet and number of bits consumed
Parse the sub-packets section of an operation packet.
The entry point for running the solutions with the ‘real’ puzzle input.
Consume the last count bits from the end of the provided vector, interpreting them as a binary
representation of a usize.
