chore: clean up day 3
This commit is contained in:
parent
7826654bce
commit
3f5ffa515c
|
@ -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
|
||||
}
|
||||
|
|
43
day03/main_test.go
Normal file
43
day03/main_test.go
Normal 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)
|
||||
}
|
|
@ -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...))
|
||||
|
||||
|
|
12
tools.go
12
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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user