chore: clean up day 3
All checks were successful
Go Bump / bump (push) Successful in 7s
Go Test / build (push) Successful in 20s

This commit is contained in:
xuu 2023-12-09 14:57:02 -07:00
parent 7826654bce
commit 3f5ffa515c
Signed by: xuu
GPG Key ID: 8B3B0604F164E04F
4 changed files with 67 additions and 15 deletions

View File

@ -2,14 +2,12 @@ package main
import ( import (
"bufio" "bufio"
"bytes"
_ "embed" _ "embed"
"fmt" "fmt"
"strconv" "strconv"
)
//go:embed input.txt aoc "go.sour.is/advent-of-code-2023"
var input []byte )
type partNumber struct { type partNumber struct {
number int number int
@ -56,10 +54,14 @@ func (tab symbolTab) scanSymbol(p partNumber) bool {
// 553079 // 553079
// 84363105 // 84363105
func main() { func main() { aoc.MustResult(aoc.Runner(run)) }
buf := bytes.NewReader(input)
scan := bufio.NewScanner(buf)
type result struct {
valuePT1 int
valuePT2 int
}
func run(scan *bufio.Scanner) (*result, error) {
parts := []partNumber{} parts := []partNumber{}
symbols := make(symbolTab) symbols := make(symbolTab)
symbolList := []*symbol{} symbolList := []*symbol{}
@ -95,6 +97,7 @@ func main() {
if v, err := strconv.Atoi(string(slice)); err == nil { if v, err := strconv.Atoi(string(slice)); err == nil {
parts = append(parts, partNumber{number: v, row: row, col: col - len(slice), end: col - 1}) parts = append(parts, partNumber{number: v, row: row, col: col - len(slice), end: col - 1})
slice = slice[:0] slice = slice[:0]
_ = slice
} }
} }
@ -119,4 +122,5 @@ func main() {
// fmt.Println(symbolList) // fmt.Println(symbolList)
fmt.Println("part1:", sum) fmt.Println("part1:", sum)
fmt.Println("part2:", sumGears) fmt.Println("part2:", sumGears)
return &result{sum, sumGears}, nil
} }

43
day03/main_test.go Normal file
View File

@ -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)
}

View File

@ -4,7 +4,6 @@ import (
"bufio" "bufio"
"cmp" "cmp"
"fmt" "fmt"
"strconv"
"strings" "strings"
aoc "go.sour.is/advent-of-code-2023" aoc "go.sour.is/advent-of-code-2023"
@ -31,13 +30,7 @@ func run(scan *bufio.Scanner) (*result, error) {
if len(text) == 0 { if len(text) == 0 {
continue continue
} }
histories = append(histories, nil) histories = append(histories, aoc.ReadStringToInts(strings.Fields(text)))
for _, s := range strings.Fields(text) {
if i, err := strconv.Atoi(s); err == nil {
histories[len(histories)-1] = append(histories[len(histories)-1], i)
}
}
log(last(histories...)) log(last(histories...))

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"strconv"
"strings" "strings"
) )
@ -51,6 +52,7 @@ func Reverse[T any](arr []T) []T {
type integer interface { type integer interface {
int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64
} }
// type float interface { // type float interface {
// complex64 | complex128 | float32 | float64 // complex64 | complex128 | float32 | float64
// } // }
@ -84,3 +86,13 @@ func LCM[T integer](integers ...T) T {
return result 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
}