/
utils.go
299 lines (259 loc) · 5.87 KB
/
utils.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package main
import (
"bufio"
"fmt"
"io/ioutil"
"log"
"os"
"strconv"
"strings"
)
//////////// Reading a file in memory
// "reads" a string into an array of strings, one per line
// useful to simulate a fileToLines in tests
func stringToLines(s string) (lines []string) {
scanner := bufio.NewScanner(strings.NewReader(s))
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
err := scanner.Err()
if err != nil {
log.Fatal(err)
}
return
}
// read the input file into a string array for feeding Parts
func fileToLines(filePath string) (lines []string) {
f, err := os.Open(filePath)
if err != nil {
log.Fatal(err)
}
defer f.Close()
scanner := bufio.NewScanner(f)
// optionally, resize scanner's capacity for lines over 64K (65536)
const maxCapacity = 1000000 // your required line length
buf := make([]byte, maxCapacity)
scanner.Buffer(buf, maxCapacity)
// end optional
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
err = scanner.Err()
if err != nil {
log.Fatal(err)
}
return
}
// read the whole input file into a single string
func fileToString(filename string) string {
bytes, err := ioutil.ReadFile(filename)
if err != nil {
fmt.Print(err)
}
return string(bytes)
}
// read the whole input file into a byte array
func fileToBytes(filename string) []byte {
bytes, err := ioutil.ReadFile(filename)
if err != nil {
fmt.Print(err)
}
return bytes
}
//////////// Exponentials / Power of integers. Use package math/big for >64bits
// IntPower calculates n to the mth power.
// it avoids messing with the rounding problems of float arthmeticm
// and is faster
func intPower(n int, m int) int {
if m <= 0 {
return 1
}
result := n
for i := 2; i <= m; i++ {
result *= n
}
return result
}
// IntPower64 calculates n to the mth power.
// version explicitely using uint64 for results
func intPower64(n int, m int) uint64 {
if m <= 0 {
return 1
}
result := uint64(n)
for i := 2; i <= m; i++ {
result *= uint64(n)
}
return result
}
func intAbs(i int) int {
if i >= 0 {
return i
} else {
return -i
}
}
//////////// Convenience functions
////// simplified functions to not bother with error handling. Just abort.
func atoi(s string) int {
i, err := strconv.Atoi(s)
if err != nil {
log.Fatal(err)
}
return i
}
// for completeness
func itoa(i int) string {
return strconv.Itoa(i)
}
////// Slice utils
func prependInt(x []int, y int) []int {
x = append(x, 0)
copy(x[1:], x)
x[0] = y
return x
}
func insertInt(list []int, i, v int) []int {
l := make([]int, len(list)+1)
copy(l[:i], list[:i])
l[i] = v
if i < len(list) {
copy(l[i+1:], list[i:])
}
return l
}
func indexOfInt(list []int, number int) (int) {
for i, v := range list {
if number == v {
return i
}
}
return -1 //not found.
}
// safe delete, keeps list order
func deleteOrderInt(list []int, number int) ([]int, bool) {
for i, v := range list {
if number == v {
l := make([]int, len(list) - 1)
copy(l, list[:i])
copy(l[i:], list[i+1:])
return l, true // Truncate slice.
}
}
return list, false // not found.
}
// safe delete, does not keep list order (faster)
func deleteInt(list []int, number int) ([]int, bool) {
l := make([]int, len(list))
copy(l, list)
return deleteFastInt(l, number)
}
// fastest delete, does not keep list order
// Should not be used if other slices point to list
func deleteFastInt(list []int, number int) ([]int, bool) {
for i, v := range list {
if number == v {
list[i] = list[len(list) - 1] // Copy last elt to index i.
return list[:len(list)-1], true // Truncate slice.
}
}
return list, false // not found.
}
func sliceIntEquals(l1, l2 []int) bool {
if len(l1) != len(l2) {
return false
}
for i, v := range l1 {
if v != l2[i] {
return false
}
}
return true
}
// Generic versions
func prependElt[T comparable](list []T, elt T) []T {
list = append(list, *new(T))
copy(list[1:], list)
list[0] = elt
return list
}
func insertElt[T comparable](list []T, i int, elt T) []T {
l := make([]T, len(list)+1)
copy(l[:i], list[:i])
l[i] = elt
if i < len(list) {
copy(l[i+1:], list[i:])
}
return l
}
func IndexOf[T comparable](collection []T, elt T) int {
for i, x := range collection {
if x == elt {
return i
}
}
return -1
}
// safe delete, keeps list order
func deleteOrderElt[T comparable](list []T, elt T) ([]T, bool) {
for i, v := range list {
if elt == v {
l := make([]T, len(list) - 1)
copy(l, list[:i])
copy(l[i:], list[i+1:])
return l, true // Truncate slice.
}
}
return list, false // not found.
}
// safe delete, does not keep list order (faster)
func deleteElt[T comparable](list []T, elt T) ([]T, bool) {
l := make([]T, len(list))
copy(l, list)
return deleteFastElt(l, elt)
}
// fastest delete, does not keep list order
// Should not be used if other slices point to list
func deleteFastElt[T comparable](list []T, elt T) ([]T, bool) {
for i, v := range list {
if elt == v {
list[i] = list[len(list) - 1] // Copy last elt to index i.
return list[:len(list)-1], true // Truncate slice.
}
}
return list, false // could not find it
}
func sliceEquals[T comparable](a []T, b []T) bool {
if len(a) != len(b) {
return false
}
for i, v := range a {
if v != b[i] {
return false
}
}
return true
}
////// VP ("Verbose Print") wrappers: print only if verbose var is true
// VP = fmt.Println
func VP(v ...interface{}) {
if verbose {
fmt.Println(v...)
}
}
// VPn (echo -n) = fmt.Print
func VPn(v ...interface{}) {
if verbose {
fmt.Print(v...)
}
}
// VPf = fmt.Printf
func VPf(f string, v ...interface{}) {
if verbose {
fmt.Printf(f, v...)
}
}
////// Convenience panic
func panicf(f string, v ...interface{}) {
panic(fmt.Sprintf(f, v...))
}