-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.go
81 lines (71 loc) · 1.35 KB
/
vector.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
package main
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
)
func main() {
input := ReadInput(os.Stdin)
length := FindLongestVector(input, byte(1))
fmt.Printf("%d\n", length)
}
const maxVectorLength = 10000
func ReadInput(reader io.Reader) []byte {
scanner := bufio.NewScanner(reader)
if !scanner.Scan() {
return nil
}
size, err := strconv.Atoi(strings.TrimSpace(scanner.Text()))
if err != nil || size > maxVectorLength {
return nil
}
result := make([]byte, 0, size)
for lineIdx := 0; scanner.Scan() && lineIdx < size; lineIdx++ {
text := strings.TrimSpace(scanner.Text())
num, err := strconv.Atoi(text)
if err != nil {
return nil
}
var b byte
if num == 1 {
b = 1
} else {
b = 0
}
result = append(result, b)
}
if size != len(result) {
return nil
}
return result
}
type MaxAggregator struct{ curMax uint }
func (m *MaxAggregator) register(n uint) {
if n > m.curMax {
m.curMax = n
}
}
type VectorRegistrar struct {
curLength uint
vectorValue byte
m MaxAggregator
}
func (v *VectorRegistrar) add(b byte) {
if b != v.vectorValue {
v.m.register(v.curLength)
v.curLength = 0
} else {
v.curLength++
v.m.register(v.curLength)
}
}
func FindLongestVector(in []byte, b byte) uint {
reg := VectorRegistrar{vectorValue: b}
for _, cur := range in {
reg.add(cur)
}
return reg.m.curMax
}