-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
200 lines (161 loc) · 4.08 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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package main
import "fmt"
import "math/rand"
import "time"
import "github.com/fatih/color"
func init() {
rand.Seed(time.Now().UnixNano())
}
type tile int
const (
Z tile = iota // Unlaid
A // 12x12
B // 12x24 horizontal
C // 12x24 vertical
D // 24x24
)
const (
ROWS = 15
COLUMNS = 60
)
var floor [ROWS][COLUMNS]tile
var tileCounts map[tile]int32
func isTileLaid(row, column int) bool {
return floor[row][column] != Z
}
func isWithinFloor(row, column int) bool {
return row >= 0 && row < ROWS && column >= 0 && column < COLUMNS
}
func canLayTile(row, column int) bool {
return isWithinFloor(row, column) && !isTileLaid(row, column)
}
func getCandidateTiles(row, column int) []tile {
var candidates []tile
if canLayTile(row, column) {
candidates = append(candidates, A)
}
if canLayTile(row, column) && canLayTile(row, column+1) {
candidates = append(candidates, B)
}
if canLayTile(row, column) && canLayTile(row+1, column) {
candidates = append(candidates, C)
}
if canLayTile(row, column) && canLayTile(row+1, column) && canLayTile(row, column+1) && canLayTile(row+1, column+1) {
candidates = append(candidates, D)
}
return candidates
}
func getLeftTile(row, column int) tile {
if !isWithinFloor(row, column-1) {
return Z
}
return floor[row][column-1]
}
func getAboveTile(row, column int) tile {
if !isWithinFloor(row-1, column) {
return Z
}
return floor[row-1][column]
}
func getProblematicTiles(candidateTiles []tile, row, column int) []tile {
var problematicTiles []tile
for _, candidateTile := range candidateTiles {
// Try to avoid picking:
//
// 1) the same tile as what's immediately to the left.
// 2) The same tile as what's immediately above.
if candidateTile == getLeftTile(row, column) || candidateTile == getAboveTile(row, column) {
problematicTiles = append(problematicTiles, candidateTile)
}
}
return problematicTiles
}
func removeProblematicTiles(candidateTiles, problematicTiles []tile) []tile {
var filteredTiles []tile
NextCandidateTile:
for _, candidateTile := range candidateTiles {
for _, problematicTile := range problematicTiles {
if problematicTile == candidateTile {
continue NextCandidateTile
}
}
filteredTiles = append(filteredTiles, candidateTile)
}
return filteredTiles
}
func layTile(row, column int, t tile) {
defer func() {
if tileCounts == nil {
tileCounts = make(map[tile]int32)
}
tileCounts[t]++
}()
switch t {
case A:
floor[row][column] = t
return
case B:
floor[row][column] = t
floor[row][column+1] = t
return
case C:
floor[row][column] = t
floor[row+1][column] = t
return
case D:
floor[row][column] = t
floor[row+1][column] = t
floor[row][column+1] = t
floor[row+1][column+1] = t
return
}
}
func makePattern() {
for currentRow := 0; currentRow < ROWS; currentRow++ {
for currentColumn := 0; currentColumn < COLUMNS; currentColumn++ {
candidateTiles := getCandidateTiles(currentRow, currentColumn)
if len(candidateTiles) == 0 {
continue
}
problematicTiles := getProblematicTiles(candidateTiles, currentRow, currentColumn)
// If at least one of the candidates is not problematic, filter out the problematic tiles; otherwise, we have no option but to pick a problematic one.
if len(candidateTiles) > len(problematicTiles) {
candidateTiles = removeProblematicTiles(candidateTiles, problematicTiles)
}
layTile(currentRow, currentColumn, candidateTiles[rand.Intn(len(candidateTiles))])
}
}
}
func getTileColor(t tile) *color.Color {
switch t {
case A:
return color.New(color.FgRed)
case B:
return color.New(color.FgBlue)
case C:
return color.New(color.FgGreen)
case D:
return color.New(color.FgYellow)
case Z:
return color.New(color.FgWhite)
}
return nil
}
func printTile(t tile) {
getTileColor(t).Add(color.Bold).Printf("%d", t)
}
func printFloor() {
for t,n := range tileCounts {
fmt.Printf("%v:%d\n", t, n)
}
for i := 0; i < ROWS; i++ {
for j := 0; j < COLUMNS; j++ {
printTile(floor[i][j])
}
fmt.Printf("\n")
}
}
func main() {
makePattern()
printFloor()
}