-
Notifications
You must be signed in to change notification settings - Fork 4
/
flask.go
151 lines (126 loc) · 2.64 KB
/
flask.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
package watersortpuzzle
import (
"errors"
"fmt"
"unsafe"
)
// Color of water piece.
// Actually it doesn't matter what exact colors there are.
// Just need to be able to compare them by ==.
// Zero-value rune is reserved for absence of Color.
type Color rune
const (
colorNone Color = 0
// invalidColor is an invalid Color
invalidColor Color = ';'
// waterPiecesPerFlask must be > 0.
waterPiecesPerFlask = 4
)
type Flask [waterPiecesPerFlask]Color
func (f *Flask) Size() int {
for i, c := range f {
if c == colorNone {
return i
}
}
return len(f)
}
func (f *Flask) Left() int {
return len(f) - f.Size()
}
func (f *Flask) IsFull() bool {
return f[len(f)-1] != colorNone
}
func (f *Flask) IsEmpty() bool {
return f[0] == colorNone
}
// Pour adds a tower of a given color to the flask.
func (f *Flask) Pour(c Color, height int) error {
if f.Left() < height {
return errors.New("cannot pour full flask")
}
if c == colorNone {
return errors.New("cannot pour none Color")
}
size := f.Size()
for i := 0; i < height; i++ {
f[size+i] = c
}
return nil
}
func (f *Flask) IsFinished() bool {
if f.IsEmpty() {
return true
}
if !f.IsFull() {
return false
}
for i := 1; i < len(f); i++ {
if f[i] != f[i-1] {
return false
}
}
return true
}
// ColorTowers returns number of color towers in flask.
func (f *Flask) ColorTowers() int {
if f.IsEmpty() {
return 0
}
towers := 1
for i := 1; i < f.Size(); i++ {
if f[i] != f[i-1] {
towers++
}
}
return towers
}
// BottomColor of flask. For empty flask returns colorNone.
func (f *Flask) BottomColor() Color {
return f[0]
}
// Top returns last tower stats.
func (f *Flask) Top() (clr Color, height int) {
for i := f.Size() - 1; i >= 0; i-- {
if f[i] == clr {
height++
continue
}
if height != 0 {
return
}
clr = f[i]
height = 1
}
return
}
// PopTop pops the last tower of one color.
func (f *Flask) PopTop() (clr Color, height int) {
clr, height = f.Top()
size := f.Size()
for i := 0; i < height; i++ {
f[size-1-i] = colorNone
}
return
}
// String representation of the flask.
func (f *Flask) String() string {
flaskSlice := f[:]
runes := *(*[]rune)(unsafe.Pointer(&flaskSlice))
return string(runes[:f.Size()])
}
// FromString initializes flask from string.
// String mustn't contain ';' and empty rune.
func (f *Flask) FromString(s string) error {
if len(s) > waterPiecesPerFlask {
return fmt.Errorf("cannot initialize flask of capacity %d from %q", waterPiecesPerFlask, s)
}
for i, r := range s {
clr := Color(r)
if clr == invalidColor || clr == colorNone {
return errors.New("invalid color provided")
}
f[i] = clr
}
return nil
}