[TASK] Day 8 code cleanup
This commit is contained in:
27
src/day8.rs
27
src/day8.rs
@@ -7,6 +7,12 @@ pub fn solve() {
|
|||||||
let part1 = execute(&program);
|
let part1 = execute(&program);
|
||||||
println!("Day 8 Part 1: {}", part1.unwrap_err().0);
|
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() {
|
for i in 0..program.len() {
|
||||||
|
|
||||||
let ins = &program[i];
|
let ins = &program[i];
|
||||||
@@ -16,24 +22,17 @@ pub fn solve() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let mut new_program = program.to_vec();
|
let mut new_program = program.to_vec();
|
||||||
if ins.op == Operation::JMP {
|
new_program[i] = Instruction {
|
||||||
new_program[i] = Instruction {
|
op: if ins.op == Operation::JMP { Operation::NOP } else { Operation::JMP },
|
||||||
op: Operation::NOP,
|
arg: ins.arg
|
||||||
arg: ins.arg
|
};
|
||||||
}
|
|
||||||
} else if ins.op == Operation::NOP {
|
|
||||||
new_program[i] = Instruction {
|
|
||||||
op: Operation::JMP,
|
|
||||||
arg: ins.arg
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let res = execute(&new_program);
|
let res = execute(&new_program);
|
||||||
if res.is_ok() {
|
if res.is_ok() {
|
||||||
let part2 = res.unwrap();
|
return res.unwrap();
|
||||||
println!("Day 8 Part 2: {}", part2);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
panic!("Couldn't find solution!");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn execute(instructions: &[Instruction]) -> Result<i32, (i32, String)> {
|
fn execute(instructions: &[Instruction]) -> Result<i32, (i32, String)> {
|
||||||
|
|||||||
Reference in New Issue
Block a user