From 3f5ffa515cb46b1d07610d001d5e830139327f3d Mon Sep 17 00:00:00 2001 From: xuu Date: Sat, 9 Dec 2023 14:57:02 -0700 Subject: [PATCH] chore: clean up day 3 --- day03/main.go | 18 +++++++++++------- day03/main_test.go | 43 +++++++++++++++++++++++++++++++++++++++++++ day09/main.go | 9 +-------- tools.go | 12 ++++++++++++ 4 files changed, 67 insertions(+), 15 deletions(-) create mode 100644 day03/main_test.go diff --git a/day03/main.go b/day03/main.go index ad2c1f8..d58e4ee 100644 --- a/day03/main.go +++ b/day03/main.go @@ -2,14 +2,12 @@ package main import ( "bufio" - "bytes" _ "embed" "fmt" "strconv" -) -//go:embed input.txt -var input []byte + aoc "go.sour.is/advent-of-code-2023" +) type partNumber struct { number int @@ -56,10 +54,14 @@ func (tab symbolTab) scanSymbol(p partNumber) bool { // 553079 // 84363105 -func main() { - buf := bytes.NewReader(input) - scan := bufio.NewScanner(buf) +func main() { aoc.MustResult(aoc.Runner(run)) } +type result struct { + valuePT1 int + valuePT2 int +} + +func run(scan *bufio.Scanner) (*result, error) { parts := []partNumber{} symbols := make(symbolTab) symbolList := []*symbol{} @@ -95,6 +97,7 @@ func main() { if v, err := strconv.Atoi(string(slice)); err == nil { parts = append(parts, partNumber{number: v, row: row, col: col - len(slice), end: col - 1}) slice = slice[:0] + _ = slice } } @@ -119,4 +122,5 @@ func main() { // fmt.Println(symbolList) fmt.Println("part1:", sum) fmt.Println("part2:", sumGears) + return &result{sum, sumGears}, nil } diff --git a/day03/main_test.go b/day03/main_test.go new file mode 100644 index 0000000..9be8b76 --- /dev/null +++ b/day03/main_test.go @@ -0,0 +1,43 @@ +package main + +import ( + "bufio" + "bytes" + _ "embed" + "testing" + + "github.com/matryer/is" +) + +//go:embed example.txt +var example []byte + +//go:embed input.txt +var input []byte + +func TestExample(t *testing.T) { + is := is.New(t) + scan := bufio.NewScanner(bytes.NewReader(example)) + + result, err := run(scan) + is.NoErr(err) + + t.Log(result) + is.Equal(result.valuePT1, int(4361)) + + is.Equal(result.valuePT2, 467835) +} + +func TestInput(t *testing.T) { + is := is.New(t) + scan := bufio.NewScanner(bytes.NewReader(input)) + + result, err := run(scan) + is.NoErr(err) + + t.Log(result.valuePT1) + is.Equal(result.valuePT1, int(553079)) + + t.Log(result.valuePT2) + is.Equal(result.valuePT2, 84363105) +} diff --git a/day09/main.go b/day09/main.go index a8d2707..49f7cfe 100644 --- a/day09/main.go +++ b/day09/main.go @@ -4,7 +4,6 @@ import ( "bufio" "cmp" "fmt" - "strconv" "strings" aoc "go.sour.is/advent-of-code-2023" @@ -31,13 +30,7 @@ func run(scan *bufio.Scanner) (*result, error) { if len(text) == 0 { continue } - histories = append(histories, nil) - - for _, s := range strings.Fields(text) { - if i, err := strconv.Atoi(s); err == nil { - histories[len(histories)-1] = append(histories[len(histories)-1], i) - } - } + histories = append(histories, aoc.ReadStringToInts(strings.Fields(text))) log(last(histories...)) diff --git a/tools.go b/tools.go index baae9ed..bbd2cf8 100644 --- a/tools.go +++ b/tools.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "path/filepath" + "strconv" "strings" ) @@ -51,6 +52,7 @@ func Reverse[T any](arr []T) []T { type integer interface { int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 } + // type float interface { // complex64 | complex128 | float32 | float64 // } @@ -84,3 +86,13 @@ func LCM[T integer](integers ...T) T { return result } + +func ReadStringToInts(fields []string) []int { + arr := make([]int, len(fields)) + for i, s := range fields { + if v, err := strconv.Atoi(s); err == nil { + arr[i] = v + } + } + return arr +}