-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
110 lines (95 loc) · 1.74 KB
/
main.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
package main
import (
"fmt"
"io/ioutil"
"strings"
"time"
)
func main() {
fileContent, err := ioutil.ReadFile("infinite-pattern")
if err != nil {
fmt.Print(err)
}
lines := strings.Split(string(fileContent), "\n")
fmt.Print(lines)
size := len(lines[0])
currentGrid := makeGrid(size)
for y, line := range lines {
for x, c := range line {
value := 0
if c == '1' {
value = 1
}
currentGrid[y][x] = value
}
}
printGrid(currentGrid)
for {
updateGrid(currentGrid)
printGrid(currentGrid)
time.Sleep(time.Second / 10)
}
}
func printGrid(grid [][]int) {
fmt.Println()
for _, row := range grid {
for _, value := range row {
if value == 0 {
fmt.Print("-")
} else {
fmt.Print("o")
}
}
fmt.Println()
}
}
func updateGrid(grid [][]int) {
futureGrid := makeGrid(len(grid))
for y, row := range grid {
for x, _ := range row {
futureGrid[y][x] = getValue(grid, x, y)
}
}
for y, row := range grid {
for x, _ := range row {
grid[y][x] = futureGrid[y][x]
}
}
}
type position struct {
x int
y int
}
func getValue(grid [][]int, x, y int) int {
cells := []position{
position{x + 1, y - 1}, position{x + 1, y}, position{x + 1, y + 1},
position{x, y - 1}, position{x, y + 1},
position{x - 1, y - 1}, position{x - 1, y}, position{x - 1, y + 1},
}
count := 0
size := len(grid)
for _, cell := range cells {
x := cell.x
y := cell.y
if x >= 0 && x < size && y >= 0 && y < size && grid[cell.y][cell.x] == 1 {
count++
}
}
if grid[y][x] == 1 {
if count == 2 || count == 3 {
return 1
}
} else {
if count == 3 {
return 1
}
}
return 0
}
func makeGrid(size int) [][]int {
grid := make([][]int, size)
for i := range grid {
grid[i] = make([]int, size)
}
return grid
}