refactor: extract read all text method to file package
This commit is contained in:
+4
-8
@@ -1,9 +1,10 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/StevanFreeborn/advent-of-code-2025/internal/file"
|
||||||
)
|
)
|
||||||
|
|
||||||
const SeparatorCharacter = ","
|
const SeparatorCharacter = ","
|
||||||
@@ -15,13 +16,8 @@ type Range struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func SolvePartOne(filePath string) int64 {
|
func SolvePartOne(filePath string) int64 {
|
||||||
bytes, readErr := os.ReadFile(filePath)
|
input := file.ReadAllText(filePath)
|
||||||
|
strRanges := strings.SplitSeq(input, SeparatorCharacter)
|
||||||
if readErr != nil {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
strRanges := strings.SplitSeq(string(bytes), SeparatorCharacter)
|
|
||||||
|
|
||||||
rngs := []Range{}
|
rngs := []Range{}
|
||||||
|
|
||||||
|
|||||||
@@ -24,3 +24,13 @@ func ReadLines(filePath string) []string {
|
|||||||
|
|
||||||
return lines
|
return lines
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ReadAllText(filePath string) string {
|
||||||
|
fileContent, readErr := os.ReadFile(filePath)
|
||||||
|
|
||||||
|
if readErr != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
return string(fileContent)
|
||||||
|
}
|
||||||
|
|||||||
@@ -19,3 +19,13 @@ func TestReadLines(t *testing.T) {
|
|||||||
t.Errorf("got %s but expected %s", result, expectedLines)
|
t.Errorf("got %s but expected %s", result, expectedLines)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestReadAllText(t *testing.T) {
|
||||||
|
expectedText := "hello\r\nworld\r\n"
|
||||||
|
|
||||||
|
result := file.ReadAllText("test.txt")
|
||||||
|
|
||||||
|
if result != expectedText {
|
||||||
|
t.Errorf("got %q but expected %q", result, expectedText)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user