Commit 3824accf authored by Kai Brandes's avatar Kai Brandes

touching indexes correct i hope

parent 53fbaeb0
use std::cmp;
fn rel_position_and_circle(pos: &u32) -> (u32, u32) {
let mut c = pos.clone() as i32;
let mut circle_nr = 0;
......@@ -23,9 +25,9 @@ fn print_vec(v: &Vec<u32>) {
}
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];
let mut direct_follower_sum = elements[elements.len() - 2];
if (circle_iter_ele_index == 1) {
direct_follower_sum += elements[elements.len() - 3];
}
println!("--> direct_follower_sum: {} ", direct_follower_sum);
......@@ -38,8 +40,27 @@ fn new_val_for(elements: &Vec<u32>, circle_iter_ele_index: u32, index_touching_e
elements.last().unwrap() + 1
}
fn next_circle_iter_target(circle_iter_count: u32)-> u32 {
circle_iter_count* 2 +1
fn next_circle_iter_target(circle_iter_count: u32) -> u32 {
circle_iter_count * 2 + 1
}
// Wieviele Element berühren das Element an der rel Position circle_iter_ele_index
// Wenn die Breite gegeben ist?
fn nr_elements_touched(circle_iter_ele_index: u32, width: u32) -> u32 {
// Iterations are the same this is one single iter
let index_in_single_iter = circle_iter_ele_index % (width + 1);
let inc_until = 2;
let dec_from = cmp::max(2, width - 1);
let max_elements_in_row = cmp::min(width, 3);
let mut ele: u32 = 0;
for i in 0..(index_in_single_iter+1) {
if(i <= inc_until) {
ele = cmp::min(max_elements_in_row,ele + 1);
}else if(i > dec_from){
ele = cmp::max(1, ele - 1);
}
}
ele
}
pub fn calc_day_3_b(search_value: u32) -> u32 {
......@@ -59,21 +80,24 @@ pub fn calc_day_3_b(search_value: u32) -> u32 {
println!("circle_iter_target: {}", circle_iter_target);
println!("############## START ##############");
while current_val <= search_value {
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);
current_val = new_val_for(&elements, circle_iter_ele_index, &touching_indexes);
elements.push(current_val);
if(circle_iter_ele_index > 0 && circle_iter_ele_index % circle_iter_target == 0) {
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 {
} else {
circle_iter_ele_index += 1;
}
}
......@@ -96,8 +120,8 @@ mod tests {
#[test]
fn it_calcs_the_first_pos() {
assert_eq!(23, calc_day_3_b(70));
// assert_eq!(4, calc_day_3_b(2));
// 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));
......@@ -112,6 +136,29 @@ mod tests {
// assert_eq!(142, calc_day_3_b(133));
}
#[test]
fn it_calcs_indexes() {
assert_eq!(1, nr_elements_touched(0, 1));
assert_eq!(1, nr_elements_touched(1, 1));
assert_eq!(1, nr_elements_touched(2, 1));
assert_eq!(1, nr_elements_touched(3, 1));
assert_eq!(1, nr_elements_touched(0, 2)); // 11
assert_eq!(2, nr_elements_touched(1, 2));
assert_eq!(2, nr_elements_touched(2, 2));
assert_eq!(1, nr_elements_touched(3, 2));
assert_eq!(2, nr_elements_touched(4, 2));
assert_eq!(2, nr_elements_touched(5, 2)); // 57
assert_eq!(1, nr_elements_touched(0, 3));
assert_eq!(2, nr_elements_touched(1, 3));
assert_eq!(3, nr_elements_touched(2, 3));
assert_eq!(2, nr_elements_touched(3, 3));
assert_eq!(1, nr_elements_touched(4, 3));
assert_eq!(2, nr_elements_touched(5, 3));
assert_eq!(3, nr_elements_touched(6, 3));
assert_eq!(2, nr_elements_touched(7, 3));
}
#[test]
fn it_calcs_some_distances() {
assert_eq!(2, calc_day_3_a(11));
......
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