Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. use maplit::hashmap;
  2.  
  3. fn p1(input: i32) -> i32 {
  4. // Find nearest square lower than input number to
  5. // determine the max value of the previous ring
  6. let mut last_ring_width = (input as f32).sqrt().floor() as i32;
  7. if last_ring_width % 2 == 0 {
  8. last_ring_width -= 1;
  9. }
  10. // println!("{}", last_ring_width);
  11. let cur_ring_width = last_ring_width + 2;
  12. let mut x = (last_ring_width as f32 / 2.0).floor() as i32;
  13. let mut y = (last_ring_width as f32 / 2.0).floor() as i32;
  14. let rest = input - last_ring_width.pow(2);
  15.  
  16. // println!("{}::", input);
  17.  
  18. // println!("\t{}, {}", x, y);
  19. if rest < cur_ring_width - 1 {
  20. // println!("\tq1");
  21. x += 1;
  22. y -= rest - 1;
  23. } else if rest < cur_ring_width + (cur_ring_width - 2) {
  24. // println!("\tq2");
  25. x -= (rest - (last_ring_width - 1)) - 1;
  26. y -= last_ring_width - 1;
  27. } else if rest < (cur_ring_width * 2) + (cur_ring_width - 2) {
  28. // println!("\tq3");
  29. x -= last_ring_width - 1;
  30. y -= rest - (cur_ring_width * 2 + last_ring_width - 2);
  31. } else {
  32. // println!("\tq4 {}", rest);
  33. x -= rest - ((cur_ring_width - 2) + cur_ring_width * 2);
  34. y += 1;
  35. }
  36. // println!("\t{}, {}", x, y);
  37. x.abs() + y.abs()
  38. }
  39.  
  40. fn main() {
  41. let tests = hashmap! {
  42. 22 => 3,
  43. 23 => 2,
  44. 24 => 3,
  45. 25 => 4,
  46. };
  47.  
  48. /* let mut keys: Vec<&i32> = tests.keys().collect();
  49. keys.sort();
  50.  
  51. for k in keys.iter() {
  52. println!("\t{} : {} => {}", k, tests[k], p1(**k));
  53. } */
  54.  
  55. // I didn't bother with a testing system beyond that ^
  56. println!("325389 => {}", p1(325489));
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement