struct Room {
packed: u16,
}
Expand description
Pack the room state into only 2 bytes.
We use 3 bits for each amphipod plus a marker bit for a maximum of 13 bits. The room is a stack with the amphipod closest to the hallway in the least significant position.
The marker bit is used to determine how full a room is and to disambiguate empty from the A
type.
Some example rooms:
- Empty room
0000000000000001
- Room with two
A
s0000000001000000
- Room with
ABCD
whereA
is closest to hallway0001011010001000
Fields§
§packed: u16
Implementations§
Source§impl Room
impl Room
Sourcefn size(self) -> usize
fn size(self) -> usize
The marker bit is always in the most significant position, so can be used to find out the size of a room.
Sourcefn open(self, kind: usize) -> bool
fn open(self, kind: usize) -> bool
A room is “open” if amphipods of that type can move to it. This means that it must be empty or only already contain amphipods of that type.
We use a multiplication by a constant to figure out the bit pattern. For example a room
with three B
s would have a bit pattern of 0000001001001001
which is the marker bit
plus B << 6 + B << 3 + B << 0 = B * 64 + B * 8 + B = B * 73.