-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
148 lines (133 loc) · 3.89 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
package main
import (
"fmt"
"github.com/runningwild/sorts/smooth"
"github.com/davecgh/go-spew/spew"
"sort"
)
type SingleSegment struct {
ballPosition int
gapSize int
previousSegment int
nextSegment int
}
type ContiguousSegments struct {
segments []SingleSegment
}
func main() {
fmt.Println("Welcome to Day3!!!")
}
func SortBalls(arrayBalls []int) []int {
sortedBalls := arrayBalls
a := smooth.IntSlice(sortedBalls[0:])
smooth.Sort(a)
// DEBUG:
// fmt.Println("SORTED: ", sortedBalls)
return sortedBalls
// return []int{1, 4, 6, 7, 10}
}
func FindContiguousSegments(sortedBalls []int) []SingleSegment {
// currentSegmentSliceStart := 0
currentSegmentPosition := -1
segments := []SingleSegment{}
for i := 0; i < len(sortedBalls); i++ {
if currentSegmentPosition == -1 {
// at the start; set it to current value
currentSegmentPosition = sortedBalls[i]
} else {
// Now test if the previousSegmentPosition is adjacent to current Ball
// if yes; move up previousSegmentPosition
if (currentSegmentPosition+1 == sortedBalls[i]) {
currentSegmentPosition++
} else {
singleSegment := SingleSegment{previousSegment: -1, nextSegment: -1}
singleSegment.ballPosition = currentSegmentPosition
// if no; create a new Segment; fill with slice of the start of the previous Segment?
// calculate Gap!
singleSegment.gapSize = sortedBalls[i] - 1 - currentSegmentPosition
// DEBUG
// fmt.Println("INDEX: ", i, " DATA: ", sortedBalls[i])
// if contiguous; extend the edgePosition
// else calculate Gap and append a new item
// FoundGap?
// append if gap is equal/higher current
// segments = append(segments, singleSegment)
// prepend
// segments = append([]SingleSegment{SingleSegment{previousSegment: -1, nextSegment: -1}}, segments ...)
// update the currentSegmentPosition
segments = SortedInsert(segments, singleSegment)
currentSegmentPosition = sortedBalls[i]
}
}
}
// DEBUG:
// fmt.Println(fmt.Sprintf("SEGMENTS: %#v", segments))
// fmt.Println(fmt.Sprintf("SEGMENTS: %v", segments))
spew.Dump(segments)
return segments
}
func CalculateGapBetweenSegments(sortedBalls *[]int) int {
return 0
}
func (s *SingleSegment) Less(f SingleSegment) bool {
fmt.Println("NODE_GAP: ", s.gapSize, " INSERT_GAP: ", f.gapSize)
if s.gapSize < f.gapSize {
return true
}
return false
}
func SortedInsert(s []SingleSegment, f SingleSegment) []SingleSegment {
l := len(s)
if l == 0 {
return append([]SingleSegment{}, f)
}
i := 0
// Iterate over until find a node where Gap is mor than it!
for ; i < len(s); i++ {
if s[i].Less(f) {
break
}
}
fmt.Println("LENGTH: ", l, " FOUND_NODEINDEX: ", i)
if i == l { // not found = new value is the smallest
return append(s[0:l], f)
}
if i == 0 { // new value is the biggest
return append([]SingleSegment{f}, s...)
}
sam := append([]SingleSegment{}, s[i:]...)
fmt.Println(fmt.Sprintf("SLICE: %v", sam))
// What's up with the belo; why not working??
// return append(s[0:l], f, s[l+1:]...)
bob := append(s[0:i], f)
// DEBUG:
fmt.Println(fmt.Sprintf("BOB: %v", bob))
fmt.Println(fmt.Sprintf("SAM_SPREAD: %v", sam))
return append(bob, sam...)
}
// https://gist.github.com/zhum/57cb45d8bbea86d87490
func OldSortedInsert(s []SingleSegment, f SingleSegment) []SingleSegment {
l := len(s)
if l == 0 {
segments := []SingleSegment{}
return append(segments, f)
}
i := sort.Search(l,
func(i int) bool {
return s[i].Less(f)
})
fmt.Println("LENGTH: ", l, " INDEX: ", i)
if i == l { // not found = new value is the smallest
return append(s[0:l], f)
}
if i == 0 { // new value is the biggest
return append([]SingleSegment{f}, s...)
}
// What's up with the belo; why not working??
// return append(s[0:l], f, s[l+1:]...)
bob := append(s[0:i], f)
// DEBUG:
fmt.Println(fmt.Sprintf("BOB: %v", bob))
fmt.Println(fmt.Sprintf("SLICE: %v", s[l:]))
return append(bob, s[i+1:]...)
}