-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspiralBoard.go
77 lines (64 loc) · 1.91 KB
/
spiralBoard.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"fmt"
)
type spiralBoard struct {
Board
}
func (b *spiralBoard) LeftEdge() int {
return -b.GetBoardSize() / 2
}
func (b *spiralBoard) RightEdge() int {
return (b.GetBoardSize() / 2) - 1
}
func (b *spiralBoard) TopEdge() int {
return -b.GetBoardSize() / 2
}
func (b *spiralBoard) BottomEdge() int {
return (b.GetBoardSize() / 2) - 1
}
// getNumber Check docs/spiral.xlsx for an explanation on the following procedure to calculate
// the value of the corresponding square in the spiral in O(1)
func (b *spiralBoard) GetNumber(row int, column int) int {
//flip the y axes. (0,0) will be in the middle of the board, with positive numbers going up
row = -row
// r is the outer ring of the spiral
r := max(abs(row), abs(column))
//accumInnerRings is the count of elements from all of the inner spirals
accumInnerRings := (4 * r * r) - (4 * r) + 1
totalOuterRing := 8 * r
edgeSide := totalOuterRing / 4
//bring (0,0) to the bottom right of the outer ring
shiftColumn := abs(column - (edgeSide / 2))
shiftRow := row + (edgeSide / 2)
var previousEdges int
var edge int
switch {
case column == r && row != -r:
//right edge of the outer spiral, except for the bottom-right corner
previousEdges = 0 * edgeSide
edge = previousEdges + shiftRow
case row == r && column != r:
//top edge of the outer spiral
previousEdges = 1 * edgeSide
edge = previousEdges + shiftColumn
case column == -r && row != r:
//left edge of the outer spiral
previousEdges = 2 * edgeSide
edge = previousEdges + (edgeSide - shiftRow)
case row == -r && column != -r:
//bottom edge of the outer spiral
previousEdges = 3 * edgeSide
edge = previousEdges + (edgeSide - shiftColumn)
}
return accumInnerRings + edge
}
func (b *spiralBoard) PrintBoard() {
half := b.GetBoardSize() / 2
for i := -half; i < half; i++ {
for j := -half; j < half; j++ {
fmt.Printf("%v ", b.GetNumber(i, j))
}
fmt.Println()
}
}