From 76583b3dda4680a83c023999a329d41a66a6c7fa Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Tue, 5 Dec 2023 19:38:55 +0000 Subject: [PATCH] refactor: add common functions to internal package It's time to add common functions to the internal 'common' package. - Add the function that read the contents from a file to a list. - Add the function that parses a list of integers from a string. ...and more to come later... --- 2023/day-1/main.go | 4 ++- 2023/day-1/part_one.go | 28 --------------- 2023/day-2/main.go | 24 ++----------- 2023/day-3/main.go | 22 ++---------- 2023/day-4/main.go | 27 ++------------ internal/common/common.go | 65 ++++++++++++++++++++++++++++++++++ internal/common/common_test.go | 33 +++++++++++++++++ 7 files changed, 110 insertions(+), 93 deletions(-) create mode 100644 internal/common/common.go create mode 100644 internal/common/common_test.go diff --git a/2023/day-1/main.go b/2023/day-1/main.go index 5b5e1af..5ce69d3 100644 --- a/2023/day-1/main.go +++ b/2023/day-1/main.go @@ -3,10 +3,12 @@ package main import ( "fmt" "log" + + "codeflow.dananglin.me.uk/apollo/advent-of-code/internal/common" ) func main() { - document, err := calibrationDocument("2023/day-1/files/input") + document, err := common.ReadFile("2023/day-1/files/input") if err != nil { log.Fatalf("Error: Unable to retrieve the calibration document; %v", err) } diff --git a/2023/day-1/part_one.go b/2023/day-1/part_one.go index 0c1840d..0e3e763 100644 --- a/2023/day-1/part_one.go +++ b/2023/day-1/part_one.go @@ -1,11 +1,5 @@ package main -import ( - "bufio" - "fmt" - "os" -) - func partOneCalculateSumCalibrationValues(document []string) int { sum := 0 @@ -31,25 +25,3 @@ func calibrationValue(line string) int { return (10 * digits[0]) + digits[len(digits)-1] } - -func calibrationDocument(filename string) ([]string, error){ - file, err := os.Open(filename) - if err != nil { - return nil, fmt.Errorf("unable to open %q; %w", filename, err) - } - defer file.Close() - - var lines []string - - scanner := bufio.NewScanner(file) - - for scanner.Scan() { - lines = append(lines, scanner.Text()) - } - - if err := scanner.Err(); err != nil { - return nil, fmt.Errorf("error scanning %q; %w", filename, err) - } - - return lines, nil -} diff --git a/2023/day-2/main.go b/2023/day-2/main.go index a9e1c1c..1413ac5 100644 --- a/2023/day-2/main.go +++ b/2023/day-2/main.go @@ -1,16 +1,16 @@ package main import ( - "bufio" "fmt" "log" - "os" + + "codeflow.dananglin.me.uk/apollo/advent-of-code/internal/common" ) func main() { filename := "2023/day-2/files/input" - games, err := readFile(filename) + games, err := common.ReadFile(filename) if err != nil { log.Fatalf("ERROR: unable to read from %s, %v\n", filename, err) } @@ -115,21 +115,3 @@ func fewestSet(sets []Set) Set { return result } - -func readFile(filename string) ([]string, error) { - file, err := os.Open(filename) - if err != nil { - return nil, fmt.Errorf("unable to open %s, %w", filename, err) - } - - defer file.Close() - - scanner := bufio.NewScanner(file) - var content []string - - for scanner.Scan() { - content = append(content, scanner.Text()) - } - - return content, nil -} diff --git a/2023/day-3/main.go b/2023/day-3/main.go index eac8a23..bea3b90 100644 --- a/2023/day-3/main.go +++ b/2023/day-3/main.go @@ -1,12 +1,12 @@ package main import ( - "bufio" "fmt" "log" - "os" "strconv" "unicode" + + "codeflow.dananglin.me.uk/apollo/advent-of-code/internal/common" ) const ( @@ -20,7 +20,7 @@ type solution struct { } func main() { - schematic, err := engineSchematic("2023/day-3/files/input") + schematic, err := common.ReadFile("2023/day-3/files/input") if err != nil { log.Fatalf("ERROR: Unable to retrieve the engine schematic; %v\n", err) } @@ -138,22 +138,6 @@ func fixTheGondola(schematic []string) (solution, error) { return answer, nil } -func engineSchematic(filename string) ([]string, error) { - file, err := os.Open(filename) - if err != nil { - return nil, fmt.Errorf("unable to open %q; %w", filename, err) - } - - scanner := bufio.NewScanner(file) - var lines []string - - for scanner.Scan() { - lines = append(lines, scanner.Text()) - } - - return lines, nil -} - func isSymbol(r rune) bool { return !unicode.IsDigit(r) && (r != dot) } diff --git a/2023/day-4/main.go b/2023/day-4/main.go index 50e6169..91af1d5 100644 --- a/2023/day-4/main.go +++ b/2023/day-4/main.go @@ -1,12 +1,12 @@ package main import ( - "bufio" "fmt" "log" - "os" "strings" "unicode" + + "codeflow.dananglin.me.uk/apollo/advent-of-code/internal/common" ) type cardResult struct { @@ -20,7 +20,7 @@ type solution struct { } func main() { - cards, err := cardPack("2023/day-4/files/input") + cards, err := common.ReadFile("2023/day-4/files/input") if err != nil { log.Fatalf("ERROR: Unable to retrieve the pack of scratchcards; %v\n", err) } @@ -149,24 +149,3 @@ func cardScore(card string) (cardResult, error) { return result, nil } - -func cardPack(filename string) ([]string, error) { - file, err := os.Open(filename) - if err != nil { - return nil, fmt.Errorf("unable to open %q; %w", filename, err) - } - defer file.Close() - - scanner := bufio.NewScanner(file) - var cards []string - - for scanner.Scan() { - cards = append(cards, scanner.Text()) - } - - if err := scanner.Err(); err != nil { - return nil, fmt.Errorf("error scanning %q; %w", filename, err) - } - - return cards, nil -} diff --git a/internal/common/common.go b/internal/common/common.go new file mode 100644 index 0000000..53863f1 --- /dev/null +++ b/internal/common/common.go @@ -0,0 +1,65 @@ +package common + +import ( + "bufio" + "fmt" + "os" + "strconv" + "unicode" +) + +func ReadFile(filename string) ([]string, error) { + file, err := os.Open(filename) + if err != nil { + return nil, fmt.Errorf("unable to open %q; %w", filename, err) + } + defer file.Close() + + scanner := bufio.NewScanner(file) + var lines []string + + for scanner.Scan() { + lines = append(lines, scanner.Text()) + } + + if err := scanner.Err(); err != nil { + return nil, fmt.Errorf("error scanning %q; %w", filename, err) + } + + return lines, nil +} + +func ParseIntList(line string) ([]int, error) { + var values []int + digitString := "" + + addValue := func(digit string) error { + if len(digit) > 0 { + value, err := strconv.Atoi(digit) + if err != nil { + return fmt.Errorf("unable to parse %q to int; %w", digit, err) + } + values = append(values, value) + } + + return nil + } + + for i, v := range line { + if unicode.IsDigit(v) { + digitString = digitString + string(v) + if i == len(line)-1 { + if err := addValue(digitString); err != nil { + return nil, fmt.Errorf("unable to add %q to the list of ints; %w", digitString, err) + } + } + } else { + if err := addValue(digitString); err != nil { + return nil, fmt.Errorf("unable to add %q to the list of ints; %w", digitString, err) + } + digitString = "" + } + } + + return values, nil +} diff --git a/internal/common/common_test.go b/internal/common/common_test.go new file mode 100644 index 0000000..8aad6dc --- /dev/null +++ b/internal/common/common_test.go @@ -0,0 +1,33 @@ +package common + +import ( + "reflect" + "testing" +) + +func TestParseIntList(t *testing.T) { + table := []struct { + list string + want []int + }{ + { + list: "87 2 435 89 100 1 0 16534", + want: []int{87, 2, 435, 89, 100, 1, 0, 16534}, + }, + { + list: "145, 834, 66, 2, 90, 123", + want: []int{145, 834, 66, 2, 90, 123}, + }, + } + + for i := range table { + got, err := ParseIntList(table[i].list) + if err != nil { + t.Fatalf("received an error after running ParseIntList(); %v", err) + } + + if !reflect.DeepEqual(got, table[i].want) { + t.Errorf("unexpected result received from ParseIntList(); want %v, got %v", table[i].want, got) + } + } +} -- 2.45.2