Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. enum Direction {
  2. Left,
  3. Up,
  4. Right,
  5. Down
  6. }
  7.  
  8. fn spiral_iter() -> impl Iterator<Item = [i32; 2]> {
  9. let mut curr = [0, 0];
  10. let mut dir = Direction::Left;
  11. let mut len = 1;
  12. let mut remaining = 1;
  13.  
  14. std::iter::from_fn(move || {
  15. let rv = curr;
  16.  
  17. remaining -= 1;
  18.  
  19. match dir {
  20. Direction::Left => {
  21. curr[0] += 1;
  22. if remaining == 0 {
  23. dir = Direction::Up;
  24. remaining = len;
  25. }
  26. }
  27. Direction::Up => {
  28. curr[1] -= 1;
  29. if remaining == 0 {
  30. dir = Direction::Right;
  31. len += 1;
  32. remaining = len;
  33. }
  34. }
  35. Direction::Right => {
  36. curr[0] -= 1;
  37. if remaining == 0 {
  38. dir = Direction::Down;
  39. remaining = len;
  40. }
  41. }
  42. Direction::Down => {
  43. curr[1] += 1;
  44. if remaining == 0 {
  45. dir = Direction::Left;
  46. len += 1;
  47. remaining = len;
  48. }
  49. }
  50. }
  51.  
  52. Some(rv)
  53. })
  54. }
  55.  
  56. fn main() {
  57. let mult_values: Vec<[i32;2]> = spiral_iter().take(23).collect();
  58. println!("{:?}", mult_values);
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement