From 6cb3230d278a57c705de40d608d4bcd776156193 Mon Sep 17 00:00:00 2001 From: Wesley Moore Date: Sat, 7 Dec 2019 14:44:38 +1100 Subject: [PATCH] Day 5 part 2 --- 2019/src/computer.rs | 50 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/2019/src/computer.rs b/2019/src/computer.rs index 870a676..7994982 100644 --- a/2019/src/computer.rs +++ b/2019/src/computer.rs @@ -10,6 +10,10 @@ enum Instruction { Multiply(Mode, Mode), Input, Output(Mode), + JumpIfTrue(Mode, Mode), + JumpIfFalse(Mode, Mode), + LessThan(Mode, Mode), + Equals(Mode, Mode), Halt, } @@ -41,6 +45,22 @@ fn decode(mut instruction: i32) -> Instruction { ), 3 => Instruction::Input, 4 => Instruction::Output(Mode::from(divmod(&mut instruction, 10))), + 5 => Instruction::JumpIfTrue( + Mode::from(divmod(&mut instruction, 10)), + Mode::from(divmod(&mut instruction, 10)), + ), + 6 => Instruction::JumpIfFalse( + Mode::from(divmod(&mut instruction, 10)), + Mode::from(divmod(&mut instruction, 10)), + ), + 7 => Instruction::LessThan( + Mode::from(divmod(&mut instruction, 10)), + Mode::from(divmod(&mut instruction, 10)), + ), + 8 => Instruction::Equals( + Mode::from(divmod(&mut instruction, 10)), + Mode::from(divmod(&mut instruction, 10)), + ), 99 => Instruction::Halt, _ => panic!("Invalid opcode: {}", opcode), } @@ -101,6 +121,36 @@ impl<'a> Memory<'a> { println!("{}", value); ip += 2; } + Instruction::JumpIfTrue(mode1, mode2) => { + if self.read(ip + 1, mode1) != 0 { + ip = self.read(ip + 2, mode2); + } else { + ip += 3; + } + } + Instruction::JumpIfFalse(mode1, mode2) => { + if self.read(ip + 1, mode1) == 0 { + ip = self.read(ip + 2, mode2); + } else { + ip += 3; + } + } + Instruction::LessThan(mode1, mode2) => { + if self.read(ip + 1, mode1) < self.read(ip + 2, mode2) { + self.write(self.read(ip + 3, Mode::Immediate), 1); + } else { + self.write(self.read(ip + 3, Mode::Immediate), 0); + } + ip += 4; + } + Instruction::Equals(mode1, mode2) => { + if self.read(ip + 1, mode1) == self.read(ip + 2, mode2) { + self.write(self.read(ip + 3, Mode::Immediate), 1); + } else { + self.write(self.read(ip + 3, Mode::Immediate), 0); + } + ip += 4; + } Instruction::Halt => break, } }