From 927fabebfc56eca9ff208f7f1a8a7782cb8ce7be Mon Sep 17 00:00:00 2001 From: xuu Date: Wed, 13 Dec 2023 08:32:37 -0700 Subject: [PATCH] chore: imporivements --- day01/main.go | 11 +-------- day02/main.go | 21 ++++++++-------- day03/main.go | 33 +++++++++++++------------ day04/main.go | 29 ++++++++-------------- day05/main.go | 37 +++++++++++----------------- day07/main.go | 26 ++++++-------------- day07/main_test.go | 18 ++++++++------ day11/main.go | 47 ++++++++++++----------------------- tools.go | 61 ++++++++++++++++++++++++++++++++++++++++++++-- 9 files changed, 145 insertions(+), 138 deletions(-) diff --git a/day01/main.go b/day01/main.go index 4877958..9764695 100644 --- a/day01/main.go +++ b/day01/main.go @@ -4,21 +4,12 @@ import ( "bufio" _ "embed" "fmt" - "os" "strings" aoc "go.sour.is/advent-of-code-2023" ) -func main() { - result, err := aoc.Runner(run) - if err != nil { - fmt.Println("ERR", err) - os.Exit(1) - } - - fmt.Println(result) -} +func main() { aoc.MustResult(aoc.Runner(run)) } type result struct { sum int diff --git a/day02/main.go b/day02/main.go index f9759c2..9537f4b 100644 --- a/day02/main.go +++ b/day02/main.go @@ -51,12 +51,10 @@ func run(scan *bufio.Scanner) (*result, error) { continue } - games = append(games, []gameResult{}) + rounds := aoc.SliceMap(func(text string) gameResult { + round := gameResult{} - for _, round := range strings.Split(text, ";") { - game := gameResult{} - - for _, result := range strings.Split(round, ",") { + for _, result := range strings.Split(text, ",") { ns, color, _ := strings.Cut(strings.TrimSpace(result), " ") n, err := strconv.Atoi(ns) if err != nil { @@ -65,16 +63,17 @@ func run(scan *bufio.Scanner) (*result, error) { switch color { case "red": - game.red = n + round.red = n case "green": - game.green = n + round.green = n case "blue": - game.blue = n + round.blue = n } - } - games[len(games)-1] = append(games[len(games)-1], game) - } + return round + }, strings.Split(text, ";")...) + + games = append(games, rounds) } aoc.Log(games) diff --git a/day03/main.go b/day03/main.go index d58e4ee..1299271 100644 --- a/day03/main.go +++ b/day03/main.go @@ -9,6 +9,8 @@ import ( aoc "go.sour.is/advent-of-code-2023" ) +func main() { aoc.MustResult(aoc.Runner(run)) } + type partNumber struct { number int row int @@ -54,7 +56,6 @@ func (tab symbolTab) scanSymbol(p partNumber) bool { // 553079 // 84363105 -func main() { aoc.MustResult(aoc.Runner(run)) } type result struct { valuePT1 int @@ -101,21 +102,23 @@ func run(scan *bufio.Scanner) (*result, error) { } } - sum := 0 - for i, p := range parts { - ok := symbols.scanSymbol(p) - parts[i].hasSymbol = ok - if ok { - sum += p.number - } - } + sum := aoc.SumIFunc( + func(i int, p partNumber) int { + ok := symbols.scanSymbol(p) + parts[i].hasSymbol = ok + if ok { + return p.number + } + return 0 + }, parts...,) - sumGears := 0 - for _, s := range symbolList { - if s.symbol == '*' && len(s.adjacentParts) == 2 { - sumGears += s.adjacentParts[0].number * s.adjacentParts[1].number - } - } + sumGears := aoc.SumFunc( + func(s *symbol) int { + if s.symbol == '*' && len(s.adjacentParts) == 2 { + return s.adjacentParts[0].number * s.adjacentParts[1].number + } + return 0 + }, symbolList...) // fmt.Println(parts) // fmt.Println(symbols) diff --git a/day04/main.go b/day04/main.go index 017529a..95cba9c 100644 --- a/day04/main.go +++ b/day04/main.go @@ -2,13 +2,12 @@ package main import ( "bufio" - "bytes" _ "embed" - "fmt" "log/slog" - "os" "strconv" "strings" + + aoc "go.sour.is/advent-of-code-2023" ) //go:embed input.txt @@ -21,22 +20,14 @@ type card struct { copies int } -func main() { - var level slog.Level - if err := level.UnmarshalText([]byte(os.Getenv("DEBUG_LEVEL"))); err == nil && level != 0 { - slog.SetDefault(slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: level}))) - } +func main() { aoc.MustResult(aoc.Runner(run)) } - buf := bytes.NewReader(input) - scan := bufio.NewScanner(buf) - - points, cards := run(scan) - - fmt.Println("points:", points) - fmt.Println("cards:", cards) +type result struct { + points int + cards int } -func run(scan *bufio.Scanner) (int, int) { +func run(scan *bufio.Scanner) (result, error) { cards := []*card{} for scan.Scan() { @@ -46,7 +37,7 @@ func run(scan *bufio.Scanner) (int, int) { continue } - num, _ := strconv.Atoi(strings.TrimSpace(strings.SplitN(pfx, " ", 2)[1])) + num := aoc.Atoi(strings.TrimSpace(strings.SplitN(pfx, " ", 2)[1])) cards = append(cards, &card{card: num}) buf := make([]rune, 0, 4) winner := true @@ -71,7 +62,7 @@ func run(scan *bufio.Scanner) (int, int) { } if len(buf) > 0 { - num, _ = strconv.Atoi(string(buf)) + num = aoc.Atoi(string(buf)) buf = buf[:0] _ = buf // ignore cards[len(cards)-1].scratch = append(cards[len(cards)-1].scratch, num) @@ -105,5 +96,5 @@ func run(scan *bufio.Scanner) (int, int) { slog.Debug("points", "card", card.card, "match", m, "score", sumPoints) } - return sumPoints, sumCards + return result{sumPoints, sumCards}, nil } diff --git a/day05/main.go b/day05/main.go index 47b7314..f3128ad 100644 --- a/day05/main.go +++ b/day05/main.go @@ -7,27 +7,19 @@ import ( "sort" "strconv" "strings" + + aoc "go.sour.is/advent-of-code-2023" + ) -func main() { - if len(os.Args) != 2 { - fmt.Fprintln(os.Stderr, "Usage: day05 FILE") - } +func main() { aoc.MustResult(aoc.Runner(run)) } - input, err := os.Open(os.Args[1]) - if err != nil { - fmt.Fprintln(os.Stderr, err) - } - - scan := bufio.NewScanner(input) - - minLocation, minRangeLocation := run(scan) - - fmt.Println("min location:", minLocation) - fmt.Println("min range location:", minRangeLocation) +type result struct { + minLocation int + minRange int } -func run(scan *bufio.Scanner) (int, int) { +func run(scan *bufio.Scanner) (result, error) { log("begin...") var seeds []int @@ -55,15 +47,14 @@ func run(scan *bufio.Scanner) (int, int) { lookup["humidity-to-location"], ) - return findMinLocation(seeds, find), FindMinRangeLocationMulti(seedRanges, find) + return result{findMinLocation(seeds, find), FindMinRangeLocationMulti(seedRanges, find)}, nil } func readSeeds(text string) ([]int, [][2]int) { - var seeds [] int + var seeds []int var seedRanges [][2]int - sp := strings.Fields(strings.TrimPrefix(text, "seeds: ")) - for i, s := range sp { - n, _ := strconv.Atoi(s) + + for i, n := range aoc.SliceMap(aoc.Atoi, strings.Fields(strings.TrimPrefix(text, "seeds: "))...) { seeds = append(seeds, n) if i%2 == 0 { @@ -121,7 +112,7 @@ func FindMinRangeLocation(ranges [][2]int, find *Finder) int { for _, s := range ranges { for i := 0; i < s[1]; i++ { - seedLocations = append(seedLocations, find.Find(s[0] + i)) + seedLocations = append(seedLocations, find.Find(s[0]+i)) } } return min(seedLocations...) @@ -164,7 +155,7 @@ func FindMinRangeLocationMulti(ranges [][2]int, find *Finder) int { seedLocations := make([]int, 0, results) expectResults := make([]struct{}, len(ranges)) for range expectResults { - r := <- resultsCh + r := <-resultsCh seedLocations = append(seedLocations, r...) } diff --git a/day07/main.go b/day07/main.go index f3d3697..91e74c3 100644 --- a/day07/main.go +++ b/day07/main.go @@ -3,29 +3,19 @@ package main import ( "bufio" "fmt" - "os" "sort" + + aoc "go.sour.is/advent-of-code-2023" ) -func main() { - if len(os.Args) != 2 { - fmt.Fprintln(os.Stderr, "Usage: day07 FILE") - } +func main() { aoc.MustResult(aoc.Runner(run)) } - input, err := os.Open(os.Args[1]) - if err != nil { - fmt.Fprintln(os.Stderr, err) - } - - scan := bufio.NewScanner(input) - - score1, score2 := run(scan) - - fmt.Println("score 1", score1) - fmt.Println("score 2", score2) +type result struct { + valuePT1 uint64 + valuePT2 uint64 } -func run(scan *bufio.Scanner) (uint64, uint64) { +func run(scan *bufio.Scanner) (result, error) { var game Game for scan.Scan() { @@ -47,7 +37,7 @@ func run(scan *bufio.Scanner) (uint64, uint64) { game.wildCard = 'J' product2 := calcProduct(game) - return product1, product2 + return result{product1, product2}, nil } var cardTypes1 = []rune{'A', 'K', 'Q', 'J', 'T', '9', '8', '7', '6', '5', '4', '3', '2'} diff --git a/day07/main_test.go b/day07/main_test.go index 19f84ee..a17aec4 100644 --- a/day07/main_test.go +++ b/day07/main_test.go @@ -54,19 +54,21 @@ func TestExample(t *testing.T) { is := is.New(t) scan := bufio.NewScanner(bytes.NewReader(example)) - score1, score2 := run(scan) - is.Equal(score1, uint64(6440)) - is.Equal(score2, uint64(5905)) + r, err := run(scan) + is.NoErr(err) + is.Equal(r.valuePT1, uint64(6440)) + is.Equal(r.valuePT2, uint64(5905)) } func TestSolution(t *testing.T) { is := is.New(t) scan := bufio.NewScanner(bytes.NewReader(input)) - score1, score2 := run(scan) - t.Log("score1", score1) - is.Equal(score1, uint64(248559379)) + r, err := run(scan) + is.NoErr(err) + t.Log("score1", r.valuePT1) + is.Equal(r.valuePT1, uint64(248559379)) - t.Log("score2", score2) - is.Equal(score2, uint64(249631254)) + t.Log("score2", r.valuePT2) + is.Equal(r.valuePT2, uint64(249631254)) } diff --git a/day11/main.go b/day11/main.go index ce3a4d1..272545f 100644 --- a/day11/main.go +++ b/day11/main.go @@ -28,11 +28,6 @@ func run(scan *bufio.Scanner) (*result, error) { text := scan.Text() m.readLine(text) } - // solution9 := m.expand(9).sumPaths() - // fmt.Println(solution9) - - // solution99 := m.expand(99).sumPaths() - // fmt.Println(solution99) return &result{ valuePT1: m.expand(1).sumPaths(), @@ -69,8 +64,8 @@ func (m *Map) String() string { n = n.Next() } - for row:=0; row 1; n-- { + p *= 2 + } + return p +} + +func ABS(i int) int { + if i < 0 { + return -i + } + return i +}