-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
157 lines (137 loc) · 2.52 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
package AOCUTILS
import (
"bufio"
"errors"
"log"
"os"
"sort"
"strconv"
"strings"
)
func Check(err error) {
if err != nil {
log.Fatal(err)
}
}
func ReadLines(filename string) []string {
file, err := os.Open(filename)
Check(err)
defer file.Close()
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
lines := make([]string, 0)
for scanner.Scan() {
line := scanner.Text()
lines = append(lines, line)
}
Check(scanner.Err())
return lines
}
func ReadString(filename string) string {
lines := ReadLines(filename)
return strings.Join(lines, "\n")
}
func SplitAndTrim(s string) []string {
return Map(strings.Split(s, "\n"), func(x string) string {
return strings.TrimSpace(x)
})
}
func SortDescending(a []int) []int {
b := a[:]
sort.Slice(b, func(i, j int) bool { return b[i] > b[j] })
return b
}
func SortAscending(a []int) []int {
b := a[:]
sort.Slice(b, func(i, j int) bool { return b[i] < b[j] })
return b
}
func Sum(a []int) int {
sum := 0
for _, e := range a {
sum += e
}
return sum
}
func ToInts(a []string) []int {
return Map(a, func(x string) int {
n, _ := strconv.Atoi(x)
return n
})
}
func Map[T any, S any](a []T, f func(x T) S) []S {
r := make([]S, 0)
for _, e := range a {
r = append(r, f(e))
}
return r
}
func Set[T comparable](a []T) []T {
hash := make(map[T]uint8)
for _, elem := range a {
hash[elem] = 1
}
set := make([]T, len(hash))
i := 0
for k := range hash {
set[i] = k
i += 1
}
return set
}
func ToIntMust(a string) int {
n, _ := strconv.Atoi(a)
return n
}
func Max[T int | int8 | int16 | int32 | int64 | float32 | float64](xs []T) T {
max := xs[0]
for _, s := range xs {
if s >= max {
max = s
}
}
return max
}
func Min[T int | int8 | int16 | int32 | int64 | float32 | float64](xs []T) T {
min := xs[0]
for _, s := range xs {
if s <= min {
min = s
}
}
return min
}
func StrToDigits(a string) []int {
l := len(a)
res := make([]int, l)
for i := range a {
res[i] = ToIntMust(string(a[i]))
}
return res
}
func Abs(x int) int {
if x < 0 {
return (-x)
}
return x
}
func PushBack[T any](xs []T, s T) []T {
return append(xs, s)
}
func PushFront[T any](xs []T, s T) []T {
return append([]T{s}, xs...)
}
func PopBack[T any](xs []T) ([]T, T, error) {
if len(xs) == 0 {
var zero T
return nil, zero, errors.New("array is empty")
}
return xs[:len(xs)-1], xs[len(xs)-1], nil
}
func PopFront[T any](xs []T) ([]T, T, error) {
if len(xs) == 0 {
var zero T
return nil, zero, errors.New("array is empty")
}
return xs[1:], xs[0], nil
}