Overengineer the intcode computer

This commit is contained in:
Vladan Popovic 2020-01-15 20:09:37 +01:00
parent 4d60fb3e23
commit 306ac5472e

View file

@ -1,25 +1,70 @@
use std; use std;
struct Params {
op1: usize,
op2: usize,
result: usize,
}
enum Instruction {
Add(Params),
Mul(Params),
GetParam(usize),
Start,
Halt,
}
struct IntcodeComputer {
memory: Vec<usize>,
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(),
}
}
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> { fn compute_intcode(p: &mut Vec<usize>) -> Vec<usize> {
for idx in (0..(p.len())).step_by(4) { let computer = IntcodeComputer::new(p);
match p[idx] { while
1 => {
let left = p[idx + 1];
let right = p[idx + 2];
let res = p[idx + 3];
p[res] = p[left] + p[right];
}
2 => {
let left = p[idx + 1];
let right = p[idx + 2];
let res = p[idx + 3];
p[res] = p[left] * p[right];
}
99 => break,
_ => panic!("Something went wrong!"),
}
}
p.to_vec()
} }
pub fn part_one() { pub fn part_one() {