Skip to content

Commit 076cf90

Browse files
committed
day01: project refactor
1 parent fe4c847 commit 076cf90

File tree

4 files changed

+27
-19
lines changed

4 files changed

+27
-19
lines changed

go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module adventofcode2021
2+
3+
go 1.16

01/data src/01/data

File renamed without changes.

01/solve.go src/01/solve.go

+2-19
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
package main
22

33
import (
4-
"bufio"
4+
"adventofcode2021/src/utils"
55
"fmt"
6-
"log"
7-
"os"
86
"strconv"
97
)
108

119
func main() {
12-
var lines = readFile()
10+
var lines = utils.ReadLines()
1311
var depths []int
1412
for _, value := range lines {
1513
if n, err := strconv.Atoi(value); err == nil {
@@ -43,18 +41,3 @@ func calcIncreases() func(int) int {
4341
return cnt
4442
}
4543
}
46-
47-
func readFile() []string {
48-
file, err := os.Open("data")
49-
if err != nil {
50-
log.Fatalf("failed opening file: %s", err)
51-
}
52-
scanner := bufio.NewScanner(file)
53-
scanner.Split(bufio.ScanLines)
54-
var txtlines []string
55-
for scanner.Scan() {
56-
txtlines = append(txtlines, scanner.Text())
57-
}
58-
file.Close()
59-
return txtlines
60-
}

src/utils/utils.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package utils
2+
3+
import (
4+
"bufio"
5+
"log"
6+
"os"
7+
)
8+
9+
func ReadLines() []string {
10+
file, err := os.Open("data")
11+
if err != nil {
12+
log.Fatalf("failed opening file: %s", err)
13+
}
14+
scanner := bufio.NewScanner(file)
15+
scanner.Split(bufio.ScanLines)
16+
var txtlines []string
17+
for scanner.Scan() {
18+
txtlines = append(txtlines, scanner.Text())
19+
}
20+
file.Close()
21+
return txtlines
22+
}

0 commit comments

Comments
 (0)