Commit e1549041 authored by Kai Brandes's avatar Kai Brandes

correct indexing for 3b

parent b4749125
......@@ -13,10 +13,65 @@ 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);
}
println!();
}
fn new_val_for(elements: &Vec<u32>, ele_index: &u32) -> u32 {
elements.last().unwrap() + 1
}
fn next_circle_iter_target(circle_iter_count: u32)-> u32 {
circle_iter_count* 2 +1
}
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 ele_index = 0;
let mut circle_iter_count = 1;
let mut circle_iter_target = next_circle_iter_target(circle_iter_count);
println!("circle_iter_target: {}", circle_iter_target);
println!("############## START ##############");
while current_val <= search_value {
println!("-----------------");
println!("index: {}", ele_index);
print_vec(&elements);
current_val = new_val_for(&elements, &ele_index);
elements.push(current_val);
if(ele_index > 0 && ele_index % circle_iter_target == 0) {
circle_iter_count += 1;
circle_iter_target = next_circle_iter_target(circle_iter_count);
ele_index = 0;
println!("circle_iter_target: {}", circle_iter_target);
}else {
ele_index += 1;
}
}
current_val
}
pub fn calc_day_3_a(position: u32) -> u32 {
let (rel_pos, circle_nr) = rel_position_and_circle(&position);
let width = 1 + circle_nr * 2;
((width / 2) as f32 - ((rel_pos + 1) % (width -1)) as f32).abs() as u32 + (width / 2)
((width / 2) as f32 - ((rel_pos + 1) % (width - 1)) as f32).abs() as u32 + (width / 2)
}
......@@ -24,6 +79,23 @@ pub fn calc_day_3_a(position: u32) -> u32 {
mod tests {
use super::*;
#[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!(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));
}
#[test]
fn it_calcs_some_distances() {
......
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