Overengineer the intcode computer

This commit is contained in:
Vladan Popovic 2020-01-15 20:09:37 +01:00
parent 4d60fb3e23
commit 306ac5472e
1 changed files with 63 additions and 18 deletions

View File

@ -1,25 +1,70 @@
use std; use std;
fn compute_intcode(p: &mut Vec<usize>) -> Vec<usize> { struct Params {
for idx in (0..(p.len())).step_by(4) { op1: usize,
match p[idx] { op2: usize,
1 => { result: usize,
let left = p[idx + 1]; }
let right = p[idx + 2];
let res = p[idx + 3]; enum Instruction {
p[res] = p[left] + p[right]; Add(Params),
} Mul(Params),
2 => { GetParam(usize),
let left = p[idx + 1]; Start,
let right = p[idx + 2]; Halt,
let res = p[idx + 3]; }
p[res] = p[left] * p[right];
} struct IntcodeComputer {
99 => break, memory: Vec<usize>,
_ => panic!("Something went wrong!"), output: Vec<usize>,
instruction: Instruction,
ip: usize, // instruction pointer
}
impl IntcodeComputer {
fn new(memory: Vec<usize>) -> Self {
IntcodeComputer {
memory,
output: vec![],
instruction: Instruction::Start,
ip: 0,
}
}
fn add(&mut self) {
let op = self.get_params();
self.memory[op.result] = self.memory[op.op1] + self.memory[op.op2]
}
fn mul(&mut self) {
let op = self.get_params();
self.memory[op.result] = self.memory[op.op1] * self.memory[op.op2]
}
fn get_params(&mut self) -> Params {
Params {
op1: self.next(),
op2: self.next(),
result: self.next(),
} }
} }
p.to_vec() fn halt(&mut self) {
self.instruction = Instruction::Halt;
}
fn next(&mut self) -> usize {
self.ip += 1;
self.memory[self.ip]
}
fn tick(&mut self) {
match self.next() {
1 => self.add(),
2 => self.mul(),
99 => self.halt(),
_ => panic!("invalid input code"),
}
}
}
fn compute_intcode(p: &mut Vec<usize>) -> Vec<usize> {
let computer = IntcodeComputer::new(p);
while
} }
pub fn part_one() { pub fn part_one() {