You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
2.1 KiB
72 lines
2.1 KiB
// #[derive(Copy, Clone)]
|
|
pub struct Data {
|
|
pub program: Vec<u32>,
|
|
}
|
|
|
|
impl Data {
|
|
pub fn new() -> Data {
|
|
Data {
|
|
program: Vec::new(),
|
|
}
|
|
}
|
|
|
|
pub fn parse(&mut self, input: String) {
|
|
for line in input.split(",") {
|
|
let sline = line.to_string();
|
|
// println!("{}", sline);
|
|
self.program.push(match sline.parse::<u32>() {
|
|
Ok(i) => i,
|
|
Err(_) => 0,
|
|
});
|
|
}
|
|
}
|
|
|
|
pub fn compute1(&mut self) -> u32 {
|
|
let mut pc: usize = 0;
|
|
while pc < self.program.len() {
|
|
let opcode = self.program[pc];
|
|
match opcode {
|
|
1 | 2 => {
|
|
if self.program.len() < pc + 3 {
|
|
break;
|
|
}
|
|
let arg1 = self.program[pc + 1] as usize;
|
|
let arg2 = self.program[pc + 2] as usize;
|
|
let res = self.program[pc + 3] as usize;
|
|
match opcode {
|
|
1 => self.program[res] = self.program[arg1] + self.program[arg2],
|
|
2 => self.program[res] = self.program[arg1] * self.program[arg2],
|
|
_ => {}
|
|
};
|
|
}
|
|
99 => break,
|
|
_ => {
|
|
println!(
|
|
"An Error happened: Instruction {} at pos {}",
|
|
self.program[pc], pc
|
|
);
|
|
break;
|
|
}
|
|
}
|
|
pc += 4;
|
|
}
|
|
self.program[0]
|
|
}
|
|
|
|
pub fn compute2(&self) -> u32 {
|
|
let mut result = 0u32;
|
|
let mut value = 0u32;
|
|
|
|
while result != 19690720 && value < 10000 {
|
|
let mut data = Data::new();
|
|
data.program = vec![0; self.program.len()];
|
|
data.program.copy_from_slice(&self.program);
|
|
data.program[1] = value / 100 as u32;
|
|
data.program[2] = value % 100 as u32;
|
|
// println!("{:?}", data.program);
|
|
result = data.compute1();
|
|
value += 1;
|
|
}
|
|
value - 1
|
|
}
|
|
}
|
|
|