-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproblem039.go
156 lines (139 loc) · 3.75 KB
/
problem039.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package problem039
import (
"bytes"
"errors"
)
type culuturePlate [][]int
const (
maxSize = 512
dead = 0
live = 1
)
func NewCuluturePlate(maxRow int, maxColumn int) *culuturePlate {
plate := make(culuturePlate, maxRow+1, maxRow+1)
for i := range plate {
plate[i] = make([]int, maxColumn+1, maxColumn+1)
}
return &plate
}
func (thisPlate *culuturePlate) copy() *culuturePlate {
plate := make(culuturePlate, len(*thisPlate), len(*thisPlate))
for rowIndex := range plate {
plate[rowIndex] = make([]int, len((*thisPlate)[rowIndex]), len((*thisPlate)[rowIndex]))
}
return &plate
}
func (thisPlate *culuturePlate) get(row int, column int) int {
if row < 0 || row >= len(*thisPlate) || column < 0 || column >= len((*thisPlate)[row]) {
return dead
}
return (*thisPlate)[row][column]
}
func (thisPlate *culuturePlate) set(row int, column int, state int) {
(*thisPlate)[row][column] = state
}
func (thisPlate *culuturePlate) countNeighbors(row int, column int) int {
neighborCoords := [][]int{
{row - 1, column - 1},
{row - 1, column},
{row - 1, column + 1},
{row, column - 1},
{row, column + 1},
{row + 1, column - 1},
{row + 1, column},
{row + 1, column + 1},
}
neighborCount := 0
for _, coordinates := range neighborCoords {
if thisPlate.get(coordinates[0], coordinates[1]) == live {
neighborCount++
}
}
return neighborCount
}
func (thisPlate *culuturePlate) getNextCellState(currentState int, neighborCount int) int {
if currentState == live {
switch {
case neighborCount < 2:
return dead
case neighborCount == 2 || neighborCount == 3:
return live
case neighborCount > 3:
return dead
}
} else {
if neighborCount == 3 {
return live
}
}
return currentState
}
func (thisPlate *culuturePlate) next() (*culuturePlate, error) {
nextPlate := thisPlate.copy()
for rowIndex := 0; rowIndex < len(*thisPlate)+1; rowIndex++ {
rowLength := 0
if rowIndex < len(*thisPlate) {
rowLength = len((*thisPlate)[rowIndex])
} else {
rowLength = len((*thisPlate)[rowIndex-1])
}
for columnIndex := 0; columnIndex < rowLength+1; columnIndex++ {
neighborCount := thisPlate.countNeighbors(rowIndex, columnIndex)
nextCellState := thisPlate.getNextCellState(thisPlate.get(rowIndex, columnIndex), neighborCount)
if rowIndex < len(*thisPlate) && columnIndex < len((*thisPlate)[rowIndex]) {
nextPlate.set(rowIndex, columnIndex, nextCellState)
} else if nextCellState == live {
if rowIndex >= maxSize || columnIndex >= maxSize {
return nil, errors.New("max size exceeded")
}
if rowIndex >= len(*nextPlate) {
var newRow []int
*nextPlate = append(*nextPlate, newRow)
}
if columnIndex >= len((*nextPlate)[rowIndex]) {
difference := columnIndex - len((*nextPlate)[rowIndex]) + 1
(*nextPlate)[rowIndex] = append((*nextPlate)[rowIndex], make([]int, difference, difference)...)
}
nextPlate.set(rowIndex, columnIndex, nextCellState)
}
}
}
return nextPlate, nil
}
func (thisPlate *culuturePlate) toString() string {
buffer := new(bytes.Buffer)
for _, row := range *thisPlate {
for _, cell := range row {
if cell == live {
buffer.WriteString("#")
} else {
buffer.WriteString(".")
}
}
buffer.WriteString("\n")
}
return buffer.String()
}
func GameOfLife(liveCellsCoords [][]int, steps int) (string, error) {
maxRow, maxColumn := 0, 0
for _, coords := range liveCellsCoords {
if coords[0] > maxRow {
maxRow = coords[0]
}
if coords[1] > maxColumn {
maxColumn = coords[1]
}
}
plate := NewCuluturePlate(maxRow, maxColumn)
for _, coords := range liveCellsCoords {
plate.set(coords[0], coords[1], live)
}
for i := 0; i < steps; i++ {
next, err := plate.next()
if err != nil {
return "", err
}
plate = next
}
return plate.toString(), nil
}