-
Notifications
You must be signed in to change notification settings - Fork 0
/
math.go
96 lines (74 loc) · 1.09 KB
/
math.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
package aoc
import "math"
type Int64Counter struct {
m map[int64]int
}
func NewInt64Counter() *Int64Counter {
return &Int64Counter{
m: map[int64]int{},
}
}
func (s *Int64Counter) Inc(v int64) {
s.m[v] = s.m[v] + 1
}
func (s *Int64Counter) MostCommon() (val int64, n int) {
var mcVal int64
var mc int
for k, v := range s.m {
if mc < v {
mcVal = k
mc = v
}
}
return mcVal, mc
}
func (s *Int64Counter) GetByN(n int) (int64, bool) {
for k, v := range s.m {
if n == v {
return k, true
}
}
return 0, false
}
func MinInt(n ...int) int {
var r int = math.MaxInt64
for _, v := range n {
if r > v {
r = v
}
}
return r
}
func MinInt64(n ...int64) int64 {
var r int64 = math.MaxInt64
for _, v := range n {
if r > v {
r = v
}
}
return r
}
func MaxInt(n ...int) int {
var r int = math.MinInt64
for _, v := range n {
if r < v {
r = v
}
}
return r
}
func MaxInt64(n ...int64) int64 {
var r int64 = math.MinInt64
for _, v := range n {
if r < v {
r = v
}
}
return r
}
func AbsInt64(n int64) int64 {
if n < 0 {
return -n
}
return n
}