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.
Trait Implementations§
impl Copy for Room
impl Eq for Room
impl StructuralPartialEq for Room
Auto Trait Implementations§
impl Freeze for Room
impl RefUnwindSafe for Room
impl Send for Room
impl Sync for Room
impl Unpin for Room
impl UnwindSafe for Room
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)