Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use maplit::hashmap;
- fn p1(input: i32) -> i32 {
- // Find nearest square lower than input number to
- // determine the max value of the previous ring
- let mut last_ring_width = (input as f32).sqrt().floor() as i32;
- if last_ring_width % 2 == 0 {
- last_ring_width -= 1;
- }
- // println!("{}", last_ring_width);
- let cur_ring_width = last_ring_width + 2;
- let mut x = (last_ring_width as f32 / 2.0).floor() as i32;
- let mut y = (last_ring_width as f32 / 2.0).floor() as i32;
- let rest = input - last_ring_width.pow(2);
- // println!("{}::", input);
- // println!("\t{}, {}", x, y);
- if rest < cur_ring_width - 1 {
- // println!("\tq1");
- x += 1;
- y -= rest - 1;
- } else if rest < cur_ring_width + (cur_ring_width - 2) {
- // println!("\tq2");
- x -= (rest - (last_ring_width - 1)) - 1;
- y -= last_ring_width - 1;
- } else if rest < (cur_ring_width * 2) + (cur_ring_width - 2) {
- // println!("\tq3");
- x -= last_ring_width - 1;
- y -= rest - (cur_ring_width * 2 + last_ring_width - 2);
- } else {
- // println!("\tq4 {}", rest);
- x -= rest - ((cur_ring_width - 2) + cur_ring_width * 2);
- y += 1;
- }
- // println!("\t{}, {}", x, y);
- x.abs() + y.abs()
- }
- fn main() {
- let tests = hashmap! {
- 22 => 3,
- 23 => 2,
- 24 => 3,
- 25 => 4,
- };
- /* let mut keys: Vec<&i32> = tests.keys().collect();
- keys.sort();
- for k in keys.iter() {
- println!("\t{} : {} => {}", k, tests[k], p1(**k));
- } */
- // I didn't bother with a testing system beyond that ^
- println!("325389 => {}", p1(325489));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement