Commit 01fca586 authored by Kai Brandes's avatar Kai Brandes

working version

parent 3824accf
......@@ -21,23 +21,25 @@ fn print_vec(v: &Vec<u32>) {
for a in v.iter() {
print!("{} ", a);
}
print!(" (?) ");
println!();
}
fn new_val_for(elements: &Vec<u32>, circle_iter_ele_index: u32, index_touching_elements: &Vec<usize>) -> u32 {
let mut direct_follower_sum = elements[elements.len() - 2];
if (circle_iter_ele_index == 1) {
direct_follower_sum += elements[elements.len() - 3];
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 touching_elements_sum: u32 = index_touching_elements.iter()
.map(|i: &usize| elements[*i])
.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);
elements.last().unwrap() + 1
touching_elements_sum + direct_follower_sum
}
fn next_circle_iter_target(circle_iter_count: u32) -> u32 {
......@@ -67,31 +69,37 @@ 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 = 2;
let mut elements: Vec<u32> = vec![1, 1, current_val.clone()] as Vec<u32>;
let mut touching_indexes: Vec<usize> = Vec::with_capacity(3);
touching_indexes.push(0);
let mut current_val = 0;
let mut elements: Vec<u32> = vec![1, 1];
let mut circle_iter_ele_index = 0;
let mut circle_iter_count = 1;
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);
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!("-----------------");
println!("index: {}", circle_iter_ele_index);
println!("fooooooooooooooooo----------------------------: {}", circle_iter_ele_index % ((circle_iter_target / 2) + 1));
println!("elementsmax----------------------------: {}", cmp::min((circle_iter_target / 2), 3));
println!("width----------------------------: {}", (circle_iter_target / 2));
println!("nr_elements_touched----------------------------: {}", nr_elements_touched(circle_iter_ele_index, (circle_iter_target / 2)));
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, circle_iter_ele_index, &touching_indexes);
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);
......@@ -102,7 +110,6 @@ pub fn calc_day_3_b(search_value: u32) -> u32 {
}
}
current_val
}
......@@ -121,19 +128,32 @@ mod tests {
#[test]
fn it_calcs_the_first_pos() {
// assert_eq!(23, calc_day_3_b(70));
assert_eq!(4, calc_day_3_b(10));
// assert_eq!(5, calc_day_3_b(4));
// assert_eq!(10, calc_day_3_b(5));
// assert_eq!(11, calc_day_3_b(10));
// assert_eq!(23, calc_day_3_b(11));
// assert_eq!(25, calc_day_3_b(23));
// assert_eq!(26, calc_day_3_b(25));
// assert_eq!(54, calc_day_3_b(26));
// assert_eq!(57, calc_day_3_b(54));
// assert_eq!(59, calc_day_3_b(57));
// assert_eq!(122, calc_day_3_b(59));
// assert_eq!(133, calc_day_3_b(122));
// assert_eq!(142, calc_day_3_b(133));
assert_eq!(11, calc_day_3_b(10));
assert_eq!(5, calc_day_3_b(4));
assert_eq!(10, calc_day_3_b(5));
assert_eq!(11, calc_day_3_b(10));
assert_eq!(23, calc_day_3_b(11));
assert_eq!(25, calc_day_3_b(23));
assert_eq!(26, calc_day_3_b(25));
assert_eq!(54, calc_day_3_b(26));
assert_eq!(57, calc_day_3_b(54));
assert_eq!(59, calc_day_3_b(57));
assert_eq!(122, calc_day_3_b(59));
assert_eq!(133, calc_day_3_b(122));
assert_eq!(142, calc_day_3_b(133));
assert_eq!(147, calc_day_3_b(142));
assert_eq!(304, calc_day_3_b(147));
assert_eq!(330, calc_day_3_b(304));
assert_eq!(351, calc_day_3_b(330));
assert_eq!(806, calc_day_3_b(800));
assert_eq!(880, calc_day_3_b(806));
assert_eq!(931, calc_day_3_b(880));
assert_eq!(957, calc_day_3_b(931));
}
#[test]
fn whhoo() {
assert_eq!(312453, calc_day_3_b(312051));
}
#[test]
......
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