From 3b86c5904483887c1dcc7e54cb1cf9096bcbe393 Mon Sep 17 00:00:00 2001 From: Stevan Freeborn <65925598+StevanFreeborn@users.noreply.github.com> Date: Wed, 3 Dec 2025 10:24:16 -0600 Subject: [PATCH] refactor: extract read all text method to file package --- cmd/02/main.go | 12 ++++-------- internal/file/file.go | 10 ++++++++++ internal/file/file_test.go | 10 ++++++++++ 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/cmd/02/main.go b/cmd/02/main.go index 6ed345d..d361ed8 100644 --- a/cmd/02/main.go +++ b/cmd/02/main.go @@ -1,9 +1,10 @@ package main import ( - "os" "strconv" "strings" + + "github.com/StevanFreeborn/advent-of-code-2025/internal/file" ) const SeparatorCharacter = "," @@ -15,13 +16,8 @@ type Range struct { } func SolvePartOne(filePath string) int64 { - bytes, readErr := os.ReadFile(filePath) - - if readErr != nil { - return 0 - } - - strRanges := strings.SplitSeq(string(bytes), SeparatorCharacter) + input := file.ReadAllText(filePath) + strRanges := strings.SplitSeq(input, SeparatorCharacter) rngs := []Range{} diff --git a/internal/file/file.go b/internal/file/file.go index b76a3fd..6154153 100644 --- a/internal/file/file.go +++ b/internal/file/file.go @@ -24,3 +24,13 @@ func ReadLines(filePath string) []string { return lines } + +func ReadAllText(filePath string) string { + fileContent, readErr := os.ReadFile(filePath) + + if readErr != nil { + return "" + } + + return string(fileContent) +} diff --git a/internal/file/file_test.go b/internal/file/file_test.go index 3c1bd1b..c81b82a 100644 --- a/internal/file/file_test.go +++ b/internal/file/file_test.go @@ -19,3 +19,13 @@ func TestReadLines(t *testing.T) { 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) + } +}