-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
53 lines (43 loc) · 950 Bytes
/
main.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
package main
import (
"encoding/csv"
"flag"
"fmt"
"log"
"os"
"strconv"
"github.com/jacksontong/aoc-2017-day-2-go/matrix"
)
func main() {
csv := flag.String("csv", "input.csv", "The input csv file")
flag.Parse()
input := readCSVFile(*csv)
answer := matrix.CalculateChecksum(input)
fmt.Printf("Answer: %d\n", answer)
}
// Read the csv input file and returns a 2d slice
func readCSVFile(path string) [][]int {
file, err := os.Open(path)
if err != nil {
log.Fatal("unable to read input file "+path, err)
}
defer file.Close()
csvReader := csv.NewReader(file)
records, err := csvReader.ReadAll()
if err != nil {
log.Fatal("unable to parse file as CSV for "+path, err)
}
out := make([][]int, len(records))
for i, row := range records {
out[i] = make([]int, len(row))
for j, str := range row {
n, err := strconv.Atoi(str)
if err != nil {
out[i][j] = 0
} else {
out[i][j] = n
}
}
}
return out
}