~xenrox/aoc2021

ref: 777f9de1c97225faed0b9aeedc7e3b3bdafcdd52 aoc2021/day09/utils.go -rw-r--r-- 547 bytes
777f9de1Thorben Günther Solve day 9 part 1 9 months ago
                                                                                
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
package day09

import (
	"bufio"
	"os"
	"strconv"
	"strings"
)

func readFile(path string) ([][]int, error) {
	var heightMap [][]int

	f, err := os.Open(path)
	if err != nil {
		return heightMap, err
	}
	defer f.Close()

	scanner := bufio.NewScanner(f)
	for scanner.Scan() {
		rowString := strings.Split(scanner.Text(), "")
		row := make([]int, len(rowString))
		for i, v := range rowString {
			row[i], err = strconv.Atoi(v)
			if err != nil {
				return heightMap, err
			}
		}

		heightMap = append(heightMap, row)
	}

	return heightMap, nil
}