[TASK] Day 8 code cleanup

This commit is contained in:
2020-12-08 11:28:22 +01:00
parent 48bea13e9d
commit f5850ceb2b

View File

@@ -7,6 +7,12 @@ pub fn solve() {
let part1 = execute(&program);
println!("Day 8 Part 1: {}", part1.unwrap_err().0);
let part2 = solve_part2(&program);
println!("Day 8 Part 2: {}", part2);
}
fn solve_part2(program: &Vec<Instruction>) -> i32 {
for i in 0..program.len() {
let ins = &program[i];
@@ -16,24 +22,17 @@ pub fn solve() {
}
let mut new_program = program.to_vec();
if ins.op == Operation::JMP {
new_program[i] = Instruction {
op: Operation::NOP,
arg: ins.arg
}
} else if ins.op == Operation::NOP {
new_program[i] = Instruction {
op: Operation::JMP,
arg: ins.arg
}
}
new_program[i] = Instruction {
op: if ins.op == Operation::JMP { Operation::NOP } else { Operation::JMP },
arg: ins.arg
};
let res = execute(&new_program);
if res.is_ok() {
let part2 = res.unwrap();
println!("Day 8 Part 2: {}", part2);
break;
return res.unwrap();
}
}
panic!("Couldn't find solution!");
}
fn execute(instructions: &[Instruction]) -> Result<i32, (i32, String)> {