Commit 0770e31a authored by Kai Brandes's avatar Kai Brandes

clean solutions for 3

parent 01fca586
......@@ -15,30 +15,16 @@ fn rel_position_and_circle(pos: &u32) -> (u32, u32) {
(c.abs() as u32 - 1, circle_nr)
}
fn print_vec(v: &Vec<u32>) {
print!("vec -> ");
for a in v.iter() {
print!("{} ", a);
}
print!(" (?) ");
println!();
}
// Calcs the sum based on direct followers and elements "touching" from previous circles
fn new_val_for(elements: &Vec<u32>, current_width: u32, circle_iter_ele_index: u32, nr_touched_elements: u32) -> u32 {
let mut direct_follower_sum = elements[elements.len() - 1];
println!("{} <-", direct_follower_sum);
if (circle_iter_ele_index % (current_width + 1) == 1) {
direct_follower_sum += elements[elements.len() - 2];
println!("{} <--", elements[elements.len() - 2]);
}
println!("--> direct_follower_sum: {} ", direct_follower_sum);
let mut touching_elements_sum = 0;
for i in 0..nr_touched_elements {
touching_elements_sum += elements[i as usize]
}
println!("--> touching_elements_sum: {} ", touching_elements_sum);
touching_elements_sum + direct_follower_sum
}
......@@ -66,9 +52,6 @@ fn nr_elements_touched(circle_iter_ele_index: u32, width: u32) -> u32 {
}
pub fn calc_day_3_b(search_value: u32) -> u32 {
println!("search_value: {}", search_value);
// We start with the first number
let mut current_val = 0;
let mut elements: Vec<u32> = vec![1, 1];
let mut circle_iter_ele_index = 0;
......@@ -76,40 +59,25 @@ pub fn calc_day_3_b(search_value: u32) -> u32 {
let mut circle_iter_target = next_circle_iter_target(circle_iter_count);
let mut elements_touched = 1;
println!("circle_iter_target: {}", circle_iter_target);
println!("############## START ##############");
while current_val <= search_value {
let current_width = (circle_iter_target / 2);
let new_elements_touched = nr_elements_touched(circle_iter_ele_index, current_width);
// Drop elements which can not anymore be touched or referenced by future calcs
if(new_elements_touched < elements_touched || elements_touched == 3) {
println!("Dropping first element, since we do not need it anymore");
elements.remove(0);
}
elements_touched = new_elements_touched;
println!("-----------------");
print_vec(&elements);
println!("rel_index: {}", circle_iter_ele_index);
println!("-----------------");
println!("current_width: {}", current_width);
println!("--> nr new elements touched: {}", new_elements_touched);
current_val = new_val_for(&elements, current_width, circle_iter_ele_index, new_elements_touched);
println!("###### New value is {} ", current_val);
elements.push(current_val);
if (circle_iter_ele_index > 0 && circle_iter_ele_index % circle_iter_target == 0) {
circle_iter_count += 1;
circle_iter_target = next_circle_iter_target(circle_iter_count);
circle_iter_ele_index = 0;
println!("###################### circle_iter_target: {}", circle_iter_target);
} else {
circle_iter_ele_index += 1;
}
}
current_val
}
......@@ -127,7 +95,6 @@ mod tests {
#[test]
fn it_calcs_the_first_pos() {
// assert_eq!(23, calc_day_3_b(70));
assert_eq!(11, calc_day_3_b(10));
assert_eq!(5, calc_day_3_b(4));
assert_eq!(10, calc_day_3_b(5));
......
......@@ -6,4 +6,6 @@ fn main() {
println!("Solution for day 1b is: {}", adventofcode_2017::results::day_1_b());
println!("Solution for day 2a is: {}", adventofcode_2017::results::day_2_a());
println!("Solution for day 2b is: {}", adventofcode_2017::results::day_2_b());
println!("Solution for day 3a is: {}", adventofcode_2017::results::day_3_a());
println!("Solution for day 3b is: {}", adventofcode_2017::results::day_3_b());
}
\ No newline at end of file
......@@ -31,4 +31,8 @@ pub fn day_2_b() -> u32 {
pub fn day_3_a() -> u32 {
return day3::calc_day_3_a(312051);
}
pub fn day_3_b() -> u32 {
return day3::calc_day_3_b(312051);
}
\ No newline at end of file
......@@ -4,3 +4,9 @@ extern crate adventofcode_2017;
fn it_shoul_assert_result_of_day3_a() {
assert_eq!(430, adventofcode_2017::results::day_3_a());
}
#[test]
fn it_shoul_assert_result_of_day3_b() {
assert_eq!(312453, adventofcode_2017::results::day_3_b());
}
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