Browse Source

Make IO functions nicer

master
Chris Smith 4 years ago
parent
commit
7519eceaf0
Signed by: Chris Smith <chris@chameth.com> GPG Key ID: 3A2D4BBDC4A3C9A9
4 changed files with 12 additions and 23 deletions
  1. 1
    1
      01/main.go
  2. 1
    1
      02/main.go
  3. 10
    11
      common/io.go
  4. 0
    10
      common/strings.go

+ 1
- 1
01/main.go View File

@@ -27,7 +27,7 @@ func fuelRequirements(input []int) (int, int) {
27 27
 }
28 28
 
29 29
 func main() {
30
-	input := common.Atoi(common.ReadLines("01/input.txt"))
30
+	input := common.ReadFileAsInts("01/input.txt")
31 31
 	part1, part2 := fuelRequirements(input)
32 32
 	fmt.Println(part1)
33 33
 	fmt.Println(part2)

+ 1
- 1
02/main.go View File

@@ -35,7 +35,7 @@ func findOutput(pool *sync.Pool, input []int, target int) int {
35 35
 }
36 36
 
37 37
 func main() {
38
-	input := common.Atoi(common.ReadCommas("02/input.txt"))
38
+	input := common.ReadCsvAsInts("02/input.txt")
39 39
 
40 40
 	pool := &sync.Pool{
41 41
 		New: func() interface{} {

+ 10
- 11
common/io.go View File

@@ -7,20 +7,19 @@ import (
7 7
 	"unicode/utf8"
8 8
 )
9 9
 
10
-// ReadLines reads all lines from the given path and returns them in a slice of strings.
10
+// ReadFileAsInts reads all lines from the given path and returns them in a slice of ints.
11 11
 // If an error occurs, the function will panic.
12
-func ReadLines(path string) []string {
13
-	return readWithScanner(path, bufio.ScanLines)
12
+func ReadFileAsInts(path string) []int {
13
+	return readIntsWithScanner(path, bufio.ScanLines)
14 14
 }
15 15
 
16
-// ReadCommas reads all data from the given path and returns a string slice
17
-// containing comma-delimited parts. If a
18
-// If an error occurs, the function will panic.
19
-func ReadCommas(path string) []string {
20
-	return readWithScanner(path, scanByCommas)
16
+// ReadCsvAsInts reads all data from the given path and returns an int slice
17
+// containing comma-delimited parts.  If an error occurs, the function will panic.
18
+func ReadCsvAsInts(path string) []int {
19
+	return readIntsWithScanner(path, scanByCommas)
21 20
 }
22 21
 
23
-func readWithScanner(path string, splitFunc bufio.SplitFunc) []string {
22
+func readIntsWithScanner(path string, splitFunc bufio.SplitFunc) []int {
24 23
 	file, err := os.Open(path)
25 24
 	if err != nil {
26 25
 		panic(err)
@@ -29,11 +28,11 @@ func readWithScanner(path string, splitFunc bufio.SplitFunc) []string {
29 28
 		_ = file.Close()
30 29
 	}()
31 30
 
32
-	var parts []string
31
+	var parts []int
33 32
 	scanner := bufio.NewScanner(file)
34 33
 	scanner.Split(splitFunc)
35 34
 	for scanner.Scan() {
36
-		parts = append(parts, scanner.Text())
35
+		parts = append(parts, MustAtoi(scanner.Text()))
37 36
 	}
38 37
 
39 38
 	if scanner.Err() != nil {

+ 0
- 10
common/strings.go View File

@@ -12,13 +12,3 @@ func MustAtoi(input string) int {
12 12
 	}
13 13
 	return res
14 14
 }
15
-
16
-// Atoi converts a slice of strings to a slice of integers.
17
-// It panics if any element couldn't be converted.
18
-func Atoi(input []string) []int {
19
-	var result []int
20
-	for _, element := range input {
21
-		result = append(result, MustAtoi(element))
22
-	}
23
-	return result
24
-}

Loading…
Cancel
Save