-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
69 lines (62 loc) · 1.52 KB
/
util.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
package aoc
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
"time"
)
// ScannerCloser is a scanner with a Close method to close the underlying file
type ScannerCloser struct {
*bufio.Scanner
io.Closer
}
// FileScanner opens a file and wraps it in a scanner. Close the file after you
// are done with the returned io.Closer
func FileScanner(p string) ScannerCloser {
f, err := os.Open(p)
if err != nil {
panic(err)
}
return ScannerCloser{bufio.NewScanner(f), f}
}
// FileLines returns all the lines in a file in a slice
func FileLines(p string) (lines []string) {
f := FileScanner(p)
for f.Scan() {
lines = append(lines, f.Text())
}
f.Close()
return lines
}
// FileInts reads all the lines of a file, parses them into ints and returns
// them as a slice
func FileInts(p string) (ints []int) {
f := FileScanner(p)
for f.Scan() {
ints = append(ints, AtoiOrBust(f.Text()))
}
f.Close()
return ints
}
// AtoiOrBust converts a string to an integer, or it panics
func AtoiOrBust(s string) int {
i, err := strconv.Atoi(strings.TrimSpace(s))
if err != nil {
panic(err)
}
return i
}
// PrintDuration prints the time since t. Call this function like this and it
// will print the run time of a function after it ends:
//
// defer aoc.PrintDuration(time.Now())
//
// This works because when you use 'defer' the arguments of a function will be
// evaluated immediately, while the function itself will only be evaluated after
// the parent function ends.
func PrintDuration(t time.Time) {
fmt.Println(time.Since(t))
}