Commit 5abbdd15 authored by Kai Brandes's avatar Kai Brandes

more math for 3a

parent 46fd3c53
......@@ -17,26 +17,7 @@ pub fn calc_day_3_a(input: String) -> u32 {
let position = input.parse::<u32>().unwrap();
let (rel_pos, circle_nr) = rel_position_and_circle(&position);
let width = 1 + circle_nr * 2;
let center = (width / 2, width / 2);
let mut start_vector = (width - 1, width - 2);
let mut direction = 0;
for p in 0..rel_pos {
if ((p + 1) % (width - 1) == 0 && p > 0) {
direction += 1;
}
if direction == 0 {
start_vector = (start_vector.0, start_vector.1 - 1)
} else if direction == 1 {
start_vector = (start_vector.0 - 1, start_vector.1)
} else if (direction == 2) {
start_vector = (start_vector.0, start_vector.1 + 1)
} else if (direction == 3) {
start_vector = (start_vector.0 + 1, start_vector.1)
}
}
((start_vector.0 as i32 - center.0 as i32).abs() + (start_vector.1 as i32 - center.1 as i32).abs()) as u32
((width / 2) as f32 - ((rel_pos + 1) % (width -1)) as f32).abs() as u32 + (width / 2)
}
......@@ -48,6 +29,10 @@ mod tests {
#[test]
fn it_calcs_some_distances() {
assert_eq!(2, calc_day_3_a(String::from("11")));
assert_eq!(3, calc_day_3_a(String::from("12")));
assert_eq!(4, calc_day_3_a(String::from("13")));
assert_eq!(2, calc_day_3_a(String::from("15")));
assert_eq!(2, calc_day_3_a(String::from("19")));
assert_eq!(3, calc_day_3_a(String::from("10")));
assert_eq!(6, calc_day_3_a(String::from("49")));
assert_eq!(6, calc_day_3_a(String::from("37")));
......
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