Enum advent_of_code_2021::day_18::SnailfishNumber[−][src]

enum SnailfishNumber {
    Num(u8),
    Pair(Box<SnailfishNumber>, Box<SnailfishNumber>),
}
Expand description

Represents a snailfish number as a binary tree

Variants

Num(u8)

Tuple Fields

0: u8

Leaf node

Pair(Box<SnailfishNumber>, Box<SnailfishNumber>)

Tuple Fields

Branch node - branches need to be boxed so that it has a constant size

Implementations

Combine the two halves into a new SnailfishNumber::Pair, then repeatedly call SnailfishNumber::check_depth, and SnailfishNumber::check_digits until neither change the tree.

Utility used by SnailfishNumber::check_depth to add one half of a pair to the next digit on the same side of the tree. dir indicates which way this number is travelling, and num is the actual digit to be added.

Helper for extracting the value when you know you’re at a leaf node.

Recursively walk the tree, if a Pair is found that is too deep, replace it with a leaf node with value 0, then pass the two halves of the pair back up so that they can be added to the next leftmost and rightmost leaves. As this is unwinding, assign the relevant side of the returned digit pair to the other half before passing the rest back up. See also SnailfishNumber::with that helps with resolving the explosion.

Recursively hunt for a leaf that is >9, i.e. not a digit, and of one is found split it into a pair, each leaf of which is half the original (rounding halves down and up respectively so that they sum to the original). Returns true if an oversize leaf was found and split, false otherwise.

Recursively combine pairs into a single number using the formula lhs x 3 + rhs x 2.

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

Parse a line of the input as a SnailfishNumber

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

This method tests for !=.

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

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.