Commit 6814a834 authored by Christopher Bohlen's avatar Christopher Bohlen

refactored solution

parent 2e4a8cab
......@@ -16,44 +16,29 @@ fn calculate_required_steps(starting_square: u32) -> u32 {
}
fn calculate_required_steps_to_port(mut starting_square: u32) -> u32 {
fn calculate_required_steps_to_port(starting_square: u32) -> u32 {
let factors = [1, 3, 5, 7];
let mut target_square = starting_square;
let mut steps = 0;
loop {
let (hit, steps_to_hit) = search(starting_square, 1);
if hit {
steps = steps + steps_to_hit;
break;
for factor in factors.iter() {
let (hit, steps_to_hit) = search_square(target_square, *factor);
if hit {
return steps + steps_to_hit
}
}
let (hit, steps_to_hit) = search(starting_square, 3);
if hit {
steps = steps + steps_to_hit;
break;
}
let (hit, steps_to_hit) = search(starting_square, 5);
if hit {
steps = steps + steps_to_hit;
break;
}
let (hit, steps_to_hit) = search(starting_square, 7);
if hit {
steps = steps + steps_to_hit;
break;
}
starting_square = starting_square - 1;
target_square = target_square - 1;
steps = steps + 1;
}
steps
}
fn search(target_square: u32, factor: u32) -> (bool, u32) {
fn search_square(target_square: u32, factor: u32) -> (bool, u32) {
let mut current_square = 1;
let mut current_mult = 0;
while current_square < target_square {
println!("factor: {}, current_square: {}, current_mult: {}, target_square: {}", factor, current_square, current_mult, target_square);
current_square = current_square + (current_mult * 8) + factor;
current_mult = current_mult + 1;
}
println!("factor: {}, current_square: {}, current_mult: {}, target_square: {}", factor, current_square, current_mult, target_square);
(current_square == target_square, current_mult)
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment