pub struct Program { /* private fields */ }
Expand description
A program that operates on a set of commands which represent Intcodes.
Implementations§
source§impl Program
impl Program
sourcepub fn new(cmds: Vec<i64>, start_params: &[i64]) -> Self
pub fn new(cmds: Vec<i64>, start_params: &[i64]) -> Self
Create a new program that operates on the provided commands. Start parameters can be given as well.
sourcepub fn run(&mut self, params: &[i64]) -> Result<i64>
pub fn run(&mut self, params: &[i64]) -> Result<i64>
Run one cycle of the program. A cycle is finished when either the position is at the end of
all commands, or an output (4
) or exit (99
) command is reached. In general this
function should be repeatedly called until is_finished
returns true
.
The program can still be run even if it’s already finished, but the output will always be zero.
Example
use aoc::y2019::intcode::{parse_input, Program};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut program = Program::new(parse_input("3,5,4,5,99,0")?, &[1]);
assert_eq!(1, program.run(&[])?);
assert!(program.is_finished());
assert_eq!(0, program.run(&[])?); // Already finished
Ok(())
}
sourcepub fn is_finished(&self) -> bool
pub fn is_finished(&self) -> bool
Check whether this program is still executable or reached the end of execution. The end is
reached when either the current position is at the end of commands or the current command is
the exit (99
) code.
Trait Implementations§
Auto Trait Implementations§
impl RefUnwindSafe for Program
impl Send for Program
impl Sync for Program
impl Unpin for Program
impl UnwindSafe for Program
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
Mutably borrows from an owned value. Read more