mirror of
https://github.com/wezm/advent-of-code.git
synced 2024-12-18 18:29:55 +00:00
Day 5 part 2
This commit is contained in:
parent
fb4c354aa9
commit
6cb3230d27
1 changed files with 50 additions and 0 deletions
|
@ -10,6 +10,10 @@ enum Instruction {
|
||||||
Multiply(Mode, Mode),
|
Multiply(Mode, Mode),
|
||||||
Input,
|
Input,
|
||||||
Output(Mode),
|
Output(Mode),
|
||||||
|
JumpIfTrue(Mode, Mode),
|
||||||
|
JumpIfFalse(Mode, Mode),
|
||||||
|
LessThan(Mode, Mode),
|
||||||
|
Equals(Mode, Mode),
|
||||||
Halt,
|
Halt,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -41,6 +45,22 @@ fn decode(mut instruction: i32) -> Instruction {
|
||||||
),
|
),
|
||||||
3 => Instruction::Input,
|
3 => Instruction::Input,
|
||||||
4 => Instruction::Output(Mode::from(divmod(&mut instruction, 10))),
|
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,
|
99 => Instruction::Halt,
|
||||||
_ => panic!("Invalid opcode: {}", opcode),
|
_ => panic!("Invalid opcode: {}", opcode),
|
||||||
}
|
}
|
||||||
|
@ -101,6 +121,36 @@ impl<'a> Memory<'a> {
|
||||||
println!("{}", value);
|
println!("{}", value);
|
||||||
ip += 2;
|
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,
|
Instruction::Halt => break,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue