Struct advent_of_code_2021::day_23::Burrow[−][src]

struct Burrow {
    len: usize,
    positions: u128,
}
Expand description

Represents a burrow as an integer that can be used as a list of 3-bit sections. 0-6 are the 7 cells in the hallway where a Amphipod can stop, the cells adjacent to each side tunnel are not represented here, and instead handled by build_states accounting for them when calculating costs. The remaining cells represent the side-tunnels, reading like a book. You can walk down a tunnel by staring at indices 7, 8, 9 or 10, and increasing by 4 each step. It is possible to represent a burrow of up to depth 8 in the u128 used.

The cells themselves use the numbers 0-4 to represent the types, 5 - 7 are unused:

  • 0 - Empty
  • 1 - Amber
  • 2 - Bronze
  • 3 - Copper
  • 4 - Desert

Fields

len: usize

The number of cells in the grid. 15 for depth 2 (part one) and 23 for depth 4 (part two)

positions: u128

Each cell maps to 3 bits in this integer

Implementations

Return the number representing the state at a given cell offset.

update the positions integer with the value in pos.

Set cell offset 1 in a 4-cell burrow to 4, original value = 4321 (100 011 010 001):

let offset = 6; // i.e. the first bit after the 3 bits of the cell.'
let bits = (1 << self.len /*4*/ * 3)       // 1 000 000 000 000
           -1;                             //   111 111 111 111

let hole = 7u128 << offset;                //   000 111 000 000

let mask = hole ^ bits;                    //   111 000 111 111

let zeroed = self.positions & mask;        //   100 011 010 001
                                           // & 111 000 111 111
                                           //   ---------------
                                           //   100 000 010 001

self.positions = zeroed | (val << offset); //   100 000 010 001
                                           // | 000 100 000 000
                                           //   ---------------
                                           //   100 100 010 001  
                                           // = 4421

Return a new burrow with the values at a and b swapped

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Output a representation of the burrow in .......BCBDDCBADBACADCA format

Parses a string in the format .......BCBDDCBADBACADCA as a Burrow. Used mostly for testing. See `parse_input for parsing the actual puzzle input

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.