-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc.go
67 lines (59 loc) · 1 KB
/
aoc.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
package aoc
import (
"log"
"os"
"strconv"
"strings"
)
func Lines(path string) []string {
bytes, err := os.ReadFile(path)
if err != nil {
log.Fatal(err)
}
return strings.Split(strings.TrimSpace(string(bytes)), "\n")
}
func Chunks(path string) [][]string {
bytes, err := os.ReadFile(path)
if err != nil {
log.Fatal(err)
}
chunks := strings.Split(strings.TrimSpace(string(bytes)), "\n\n")
split := make([][]string, len(chunks))
for i, chunk := range chunks {
split[i] = strings.Split(chunk, "\n")
}
return split
}
func Atoi(s string) int {
i, err := strconv.Atoi(s)
if err != nil {
log.Fatal(err)
}
return i
}
func Hextoi(hex string) int {
i64, err := strconv.ParseInt(hex, 16, 64)
if err != nil {
log.Fatal(err)
}
return int(i64)
}
func GCD(a, b int) int {
for b != 0 {
a, b = b, a%b
}
return a
}
func LCM(nums []int) int {
lcm := 1
for i := 0; i < len(nums); i++ {
lcm *= nums[i] / GCD(lcm, nums[i])
}
return lcm
}
func Abs(x int) int {
if x > 0 {
return x
}
return -x
}