fork download
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "math"
  6. )
  7.  
  8. func main() {
  9. Spiralizer(1)
  10. Spiralizer(2)
  11. Spiralizer(3)
  12. Spiralizer(4)
  13. Spiralizer(5)
  14. Spiralizer(6)
  15. Spiralizer(7)
  16. Spiralizer(8)
  17. Spiralizer(9)
  18. Spiralizer(10)
  19. Spiralizer(11)
  20. Spiralizer(12)
  21. }
  22.  
  23. func Spiralizer(n int) {
  24. if n == 1 {
  25. fmt.Printf("1\n\n")
  26. return
  27. }
  28.  
  29. // Create grid
  30. grid := make([][]int, n)
  31.  
  32. for i := range grid {
  33. grid[i] = make([]int, n)
  34. }
  35.  
  36. // Middle of the spiral
  37. middle := int(math.Ceil(float64(n-1) / 2))
  38.  
  39. for i := range grid {
  40. for j := range grid {
  41. // For the first row, just increment each element
  42. if i == 0 {
  43. grid[i][j] = j + 1
  44. }
  45.  
  46. // Populate a diagonal
  47. if j == i-1 && i <= middle {
  48. grid[i][j] = (n - i) * (4 * i)
  49. }
  50.  
  51. // Populate the left column
  52. if i > 1 && j == 0 {
  53. grid[i][j] = grid[i-1][j] - 1
  54. }
  55.  
  56. // Populate the bottom row
  57. if i == n-1 && j > 0 {
  58. grid[i][j] = grid[i][j-1] - 1
  59. }
  60.  
  61. // Populate the right column
  62. if i > 0 && j == n-1 {
  63. grid[i][j] = grid[i-1][j] + 1
  64. }
  65.  
  66. // Populate the rest of the easy rows
  67. if j >= i && j < n-i && j > 0 {
  68. grid[i][j] = grid[i][j-1] + 1
  69. }
  70.  
  71. // Populate columns in the top right quadrant
  72. if grid[i][j] == 0 &&
  73. i <= n-(n-j) &&
  74. j > middle {
  75. grid[i][j] = grid[i-1][j] + 1
  76. }
  77.  
  78. // Populate empty columns in the top left quadrant
  79. if grid[i][j] == 0 &&
  80. j < middle &&
  81. i < n-j {
  82. grid[i][j] = grid[i-1][j] - 1
  83. }
  84.  
  85. // Populate remaining rows
  86. if grid[i][j] == 0 {
  87. grid[i][j] = grid[i][j-1] - 1
  88. }
  89. }
  90. }
  91.  
  92. PrettyPrint(grid)
  93. }
  94.  
  95. func PrettyPrint(a [][]int) {
  96. size := len(a)
  97. maxspaces := int(math.Log10(float64(size * size)))
  98.  
  99. for i := range a {
  100. for j := range a {
  101. for k := 0; k < maxspaces-int(math.Log10(float64(a[i][j]))); k++ {
  102. fmt.Printf(" ")
  103. }
  104. fmt.Printf("%d ", a[i][j])
  105. }
  106. fmt.Printf("\n")
  107. }
  108. fmt.Printf("\n")
  109. }
Success #stdin #stdout 0s 2988KB
stdin
Standard input is empty
stdout
1

1 2 
4 3 

1 2 3 
8 9 4 
7 6 5 

 1  2  3  4 
12 13 14  5 
11 16 15  6 
10  9  8  7 

 1  2  3  4  5 
16 17 18 19  6 
15 24 25 20  7 
14 23 22 21  8 
13 12 11 10  9 

 1  2  3  4  5  6 
20 21 22 23 24  7 
19 32 33 34 25  8 
18 31 36 35 26  9 
17 30 29 28 27 10 
16 15 14 13 12 11 

 1  2  3  4  5  6  7 
24 25 26 27 28 29  8 
23 40 41 42 43 30  9 
22 39 48 49 44 31 10 
21 38 47 46 45 32 11 
20 37 36 35 34 33 12 
19 18 17 16 15 14 13 

 1  2  3  4  5  6  7  8 
28 29 30 31 32 33 34  9 
27 48 49 50 51 52 35 10 
26 47 60 61 62 53 36 11 
25 46 59 64 63 54 37 12 
24 45 58 57 56 55 38 13 
23 44 43 42 41 40 39 14 
22 21 20 19 18 17 16 15 

 1  2  3  4  5  6  7  8  9 
32 33 34 35 36 37 38 39 10 
31 56 57 58 59 60 61 40 11 
30 55 72 73 74 75 62 41 12 
29 54 71 80 81 76 63 42 13 
28 53 70 79 78 77 64 43 14 
27 52 69 68 67 66 65 44 15 
26 51 50 49 48 47 46 45 16 
25 24 23 22 21 20 19 18 17 

  1   2   3   4   5   6   7   8   9  10 
 36  37  38  39  40  41  42  43  44  11 
 35  64  65  66  67  68  69  70  45  12 
 34  63  84  85  86  87  88  71  46  13 
 33  62  83  96  97  98  89  72  47  14 
 32  61  82  95 100  99  90  73  48  15 
 31  60  81  94  93  92  91  74  49  16 
 30  59  80  79  78  77  76  75  50  17 
 29  58  57  56  55  54  53  52  51  18 
 28  27  26  25  24  23  22  21  20  19 

  1   2   3   4   5   6   7   8   9  10  11 
 40  41  42  43  44  45  46  47  48  49  12 
 39  72  73  74  75  76  77  78  79  50  13 
 38  71  96  97  98  99 100 101  80  51  14 
 37  70  95 112 113 114 115 102  81  52  15 
 36  69  94 111 120 121 116 103  82  53  16 
 35  68  93 110 119 118 117 104  83  54  17 
 34  67  92 109 108 107 106 105  84  55  18 
 33  66  91  90  89  88  87  86  85  56  19 
 32  65  64  63  62  61  60  59  58  57  20 
 31  30  29  28  27  26  25  24  23  22  21 

  1   2   3   4   5   6   7   8   9  10  11  12 
 44  45  46  47  48  49  50  51  52  53  54  13 
 43  80  81  82  83  84  85  86  87  88  55  14 
 42  79 108 109 110 111 112 113 114  89  56  15 
 41  78 107 128 129 130 131 132 115  90  57  16 
 40  77 106 127 140 141 142 133 116  91  58  17 
 39  76 105 126 139 144 143 134 117  92  59  18 
 38  75 104 125 138 137 136 135 118  93  60  19 
 37  74 103 124 123 122 121 120 119  94  61  20 
 36  73 102 101 100  99  98  97  96  95  62  21 
 35  72  71  70  69  68  67  66  65  64  63  22 
 34  33  32  31  30  29  28  27  26  25  24  23