refactor: add common functions to internal package
All checks were successful
/ test (pull_request) Successful in 25s
All checks were successful
/ test (pull_request) Successful in 25s
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...
This commit is contained in:
parent
904a2919f6
commit
76583b3dda
7 changed files with 110 additions and 93 deletions
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
65
internal/common/common.go
Normal file
65
internal/common/common.go
Normal file
|
@ -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
|
||||
}
|
33
internal/common/common_test.go
Normal file
33
internal/common/common_test.go
Normal file
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue