struct Computer {
register_a: usize,
register_b: usize,
register_c: usize,
program: Vec<u8>,
instruction_pointer: usize,
}
Expand description
Represents a computer and the program it will run
Fields§
§register_a: usize
§register_b: usize
§register_c: usize
§program: Vec<u8>
§instruction_pointer: usize
Implementations§
source§impl Computer
impl Computer
sourcefn with_register_a(&self, value: usize) -> Computer
fn with_register_a(&self, value: usize) -> Computer
Create a copy of this computer with the A register set to the provided value
sourcefn next_instruction(&self) -> Option<(u8, u8)>
fn next_instruction(&self) -> Option<(u8, u8)>
Read the instruction at the instruction_pointer
, and the next number as the operand
sourcefn deref_combo(&self, operand: u8) -> usize
fn deref_combo(&self, operand: u8) -> usize
Combo operands 0 through 3 represent literal values 0 through 3. Combo operand 4 represents the value of register A. Combo operand 5 represents the value of register B. Combo operand 6 represents the value of register C. Combo operand 7 is reserved and will not appear in valid programs.
sourcefn adv(&mut self, operand: u8)
fn adv(&mut self, operand: u8)
The adv instruction (opcode 0) performs division. The numerator is the value in the A register. The denominator is found by raising 2 to the power of the instruction’s combo operand. (So, an operand of 2 would divide A by 4 (2^2) ; an operand of 5 would divide A by 2^B.) The result of the division operation is truncated to an integer and then written to the A register
sourcefn bxl(&mut self, operand: u8)
fn bxl(&mut self, operand: u8)
The bxl instruction (opcode 1) calculates the bitwise XOR of register B and the instruction’s literal operand, then stores the result in register B.
sourcefn bst(&mut self, operand: u8)
fn bst(&mut self, operand: u8)
The bst instruction (opcode 2) calculates the value of its combo operand modulo 8 (thereby keeping only its lowest 3 bits), then writes that value to the B register.
sourcefn jnz(&mut self, operand: u8)
fn jnz(&mut self, operand: u8)
The jnz instruction (opcode 3) does nothing if the A register is 0. However, if the A register is not zero, it jumps by setting the instruction pointer to the value of its literal operand; if this instruction jumps, the instruction pointer is not increased by 2 after this instruction.
sourcefn bxc(&mut self, _: u8)
fn bxc(&mut self, _: u8)
The bxc instruction (opcode 4) calculates the bitwise XOR of register B and register C, then stores the result in register B. (For legacy reasons, this instruction reads an operand but ignores it.)
sourcefn out(&mut self, operand: u8) -> u8
fn out(&mut self, operand: u8) -> u8
The out instruction (opcode 5) calculates the value of its combo operand modulo 8, then outputs that value. (If a program outputs multiple values, they are separated by commas.)
sourcefn bdv(&mut self, operand: u8)
fn bdv(&mut self, operand: u8)
The bdv instruction (opcode 6) works exactly like the adv instruction except that the result is stored in the B register. (The numerator is still read from the A register.)
Trait Implementations§
impl Eq for Computer
impl StructuralPartialEq for Computer
Auto Trait Implementations§
impl Freeze for Computer
impl RefUnwindSafe for Computer
impl Send for Computer
impl Sync for Computer
impl Unpin for Computer
impl UnwindSafe for Computer
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
)§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.