-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenge03.go
78 lines (64 loc) · 1.49 KB
/
challenge03.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
package main
import (
"fmt"
"log"
"os"
"sort"
"strings"
)
// Remove all new lines and spaces.
func formatContent(c []byte) []string {
replacer := strings.NewReplacer("\n", "", " ", "")
r := replacer.Replace(string(c))
t := strings.Trim(r, "[]")
return strings.Split(t, ",")
}
func main() {
file, err := os.ReadFile("colors.txt")
if err != nil {
log.Fatal(err)
}
colors := formatContent(file)
result := make([][]string, 0)
tmpRes := make([]string, 0)
for i := 0; i < len(colors); i++ {
// Tmp slice is empty
if len(tmpRes) == 0 {
tmpRes = append(tmpRes, colors[i])
continue
}
// Tmp slice have one element
if len(tmpRes) == 1 {
if tmpRes[len(tmpRes)-1] == colors[i] {
result = append(result, tmpRes)
tmpRes = make([]string, 0)
tmpRes = append(tmpRes, colors[i])
continue
} else {
tmpRes = append(tmpRes, colors[i])
continue
}
}
if tmpRes[len(tmpRes)-2] == colors[i] && tmpRes[len(tmpRes)-1] != colors[i] {
tmpRes = append(tmpRes, colors[i])
continue
}
if tmpRes[len(tmpRes)-1] == colors[i] {
result = append(result, tmpRes)
tmpRes = make([]string, 0)
tmpRes = append(tmpRes, colors[i])
continue
}
result = append(result, tmpRes)
tmpRes = tmpRes[len(tmpRes)-1:]
tmpRes = append(tmpRes, colors[i])
}
sort.Slice(result, func(i, j int) bool {
l1 := len(result[i])
l2 := len(result[j])
return l1 < l2
})
fmt.Println(result)
fmt.Println(len(result[len(result)-1]))
fmt.Println(result[len(result)-1])
}