From 4ffe17fe12259178ecf59d4c3b94054c48943156 Mon Sep 17 00:00:00 2001 From: Gabriel Bizdoc Date: Mon, 8 Dec 2025 05:18:10 +0200 Subject: [PATCH] refactor aoc.utils --- {aocutils => aoc}/utils.go | 2 +- year25/day1.go | 6 +++--- year25/day2.go | 12 ++++++------ year25/day3.go | 6 +++--- year25/day4.go | 4 ++-- year25/day5.go | 19 +++++++++++++------ year25/day6.go | 6 +++--- year25/day7.go | 8 ++++---- 8 files changed, 35 insertions(+), 28 deletions(-) rename {aocutils => aoc}/utils.go (99%) diff --git a/aocutils/utils.go b/aoc/utils.go similarity index 99% rename from aocutils/utils.go rename to aoc/utils.go index 2636804..0073787 100644 --- a/aocutils/utils.go +++ b/aoc/utils.go @@ -1,4 +1,4 @@ -package aocutils +package aoc import ( "bufio" diff --git a/year25/day1.go b/year25/day1.go index 9153e2e..04066ad 100644 --- a/year25/day1.go +++ b/year25/day1.go @@ -10,7 +10,7 @@ import ( "git.bizdoc.ro/gabi-public/Advent-of-Code.git/aocutils" ) -func Day1Part1(ctx aocutils.Context) (any, error) { +func Day1Part1(ctx aoc.Context) (any, error) { dial := 50 solution := 0 for pair := range day1GetPairs(ctx.Body) { @@ -28,7 +28,7 @@ func Day1Part1(ctx aocutils.Context) (any, error) { return solution, nil } -func Day1Part2Slow(ctx aocutils.Context) (any, error) { +func Day1Part2Slow(ctx aoc.Context) (any, error) { dial := 50 solution := 0 for pair := range day1GetPairs(ctx.Body) { @@ -46,7 +46,7 @@ func Day1Part2Slow(ctx aocutils.Context) (any, error) { return solution, nil } -func Day1Part2Fast(ctx aocutils.Context) (any, error) { +func Day1Part2Fast(ctx aoc.Context) (any, error) { dial := 50 solution := 0 for pair := range day1GetPairs(ctx.Body) { diff --git a/year25/day2.go b/year25/day2.go index b5c52ee..771581a 100644 --- a/year25/day2.go +++ b/year25/day2.go @@ -49,7 +49,7 @@ func (s *day2Scanner) Bounds() (int, int) { return s.a, s.b } -func Day2Part1Simple(ctx aocutils.Context) (any, error) { +func Day2Part1Simple(ctx aoc.Context) (any, error) { input, err := ctx.BodyString() if err != nil { return nil, fmt.Errorf("failed to read input: %s", err) @@ -93,7 +93,7 @@ func Day2Part1Simple(ctx aocutils.Context) (any, error) { return sum, nil } -func Day2Part2Simple(ctx aocutils.Context) (any, error) { +func Day2Part2Simple(ctx aoc.Context) (any, error) { input, err := ctx.BodyString() if err != nil { return nil, fmt.Errorf("failed to read input: %s", err) @@ -153,8 +153,8 @@ func Day2Part2Simple(ctx aocutils.Context) (any, error) { return sum, nil } -func Day2Part1(ctx aocutils.Context) (any, error) { - scanner := &day2Scanner{scanner: ctx.Scanner(aocutils.SplitComma), sep: '-'} +func Day2Part1(ctx aoc.Context) (any, error) { + scanner := &day2Scanner{scanner: ctx.Scanner(aoc.SplitComma), sep: '-'} var sum int @@ -178,8 +178,8 @@ func Day2Part1(ctx aocutils.Context) (any, error) { return sum, scanner.Err() } -func Day2Part2(ctx aocutils.Context) (any, error) { - scanner := &day2Scanner{scanner: ctx.Scanner(aocutils.SplitComma), sep: '-'} +func Day2Part2(ctx aoc.Context) (any, error) { + scanner := &day2Scanner{scanner: ctx.Scanner(aoc.SplitComma), sep: '-'} var sum int for scanner.Scan() { diff --git a/year25/day3.go b/year25/day3.go index 5cc8f70..22b3885 100644 --- a/year25/day3.go +++ b/year25/day3.go @@ -9,13 +9,13 @@ import ( "git.bizdoc.ro/gabi-public/Advent-of-Code.git/aocutils" ) -func Day3Part1(ctx aocutils.Context) (any, error) { +func Day3Part1(ctx aoc.Context) (any, error) { return Day3(ctx, 2) } -func Day3Part2(ctx aocutils.Context) (any, error) { +func Day3Part2(ctx aoc.Context) (any, error) { return Day3(ctx, 12) } -func Day3(ctx aocutils.Context, size int) (int64, error) { +func Day3(ctx aoc.Context, size int) (int64, error) { scanner := ctx.Scanner(bufio.ScanLines) var sum int64 diff --git a/year25/day4.go b/year25/day4.go index 6727049..192c597 100644 --- a/year25/day4.go +++ b/year25/day4.go @@ -9,7 +9,7 @@ import ( const Day4PaperRoll = '@' -func Day4Part1(ctx aocutils.Context) (any, error) { +func Day4Part1(ctx aoc.Context) (any, error) { // 4 rolls in 8 adjacent positions const maxAdjacentRolls = 4 @@ -39,7 +39,7 @@ func Day4Part1(ctx aocutils.Context) (any, error) { return accessibleRolls, nil } -func Day4Part2(ctx aocutils.Context) (any, error) { +func Day4Part2(ctx aoc.Context) (any, error) { // 4 rolls in 8 adjacent positions const maxAdjacentRolls = 4 diff --git a/year25/day5.go b/year25/day5.go index 2624bd6..79669d8 100644 --- a/year25/day5.go +++ b/year25/day5.go @@ -2,6 +2,7 @@ package year25 import ( "bufio" + "cmp" "fmt" "iter" "slices" @@ -20,7 +21,7 @@ type MergedIntervals struct { intervals []*Day5Interval } -func (m *MergedIntervals) Put(newInterval Day5Interval) { +func (m *MergedIntervals) Put1(newInterval Day5Interval) { for item := range m.Items() { if newInterval.Start <= item.End && newInterval.End >= item.Start { newInterval.Start = min(newInterval.Start, item.Start) @@ -39,7 +40,7 @@ func (m *MergedIntervals) Put(newInterval Day5Interval) { m.intervals = append(m.intervals, &newInterval) } -func (m *MergedIntervals) Put2(newInterval Day5Interval) { +func (m *MergedIntervals) Put(newInterval Day5Interval) { // fast search start, end for item := range m.Items() { if newInterval.Start <= item.End && newInterval.End >= item.Start { @@ -52,8 +53,14 @@ func (m *MergedIntervals) Put2(newInterval Day5Interval) { } } - // insert in order - m.intervals = append(m.intervals, &newInterval) + m.intervals = slices.DeleteFunc(m.intervals, func(i *Day5Interval) bool { + return i.isRemoved + }) + + i, _ := slices.BinarySearchFunc(m.intervals, &newInterval, func(interval *Day5Interval, d *Day5Interval) int { + return cmp.Compare(interval.Start, newInterval.Start) + }) + m.intervals = slices.Insert(m.intervals, i, &newInterval) } func (m *MergedIntervals) CheckItem(itemId int64) (*Day5Interval, bool) { @@ -102,7 +109,7 @@ func Day5ParseIntervals(s *bufio.Scanner) (intervals *MergedIntervals, err error return intervals, s.Err() } -func Day5Part1(ctx aocutils.Context) (int, error) { +func Day5Part1(ctx aoc.Context) (int, error) { scanner := ctx.Scanner() intervals, err := Day5ParseIntervals(scanner) if err != nil { @@ -126,7 +133,7 @@ func Day5Part1(ctx aocutils.Context) (int, error) { return freshIngredients, scanner.Err() } -func Day5Part2(ctx aocutils.Context) (solution int64, _ error) { +func Day5Part2(ctx aoc.Context) (solution int64, _ error) { intervals, err := Day5ParseIntervals(ctx.Scanner()) if err != nil { return 0, fmt.Errorf("day5: failed to parse intervals %w", err) diff --git a/year25/day6.go b/year25/day6.go index 092b44a..bbf110f 100644 --- a/year25/day6.go +++ b/year25/day6.go @@ -12,7 +12,7 @@ import ( "git.bizdoc.ro/private/devkit.git/collections/geometry/v2" ) -func Day6Part1(c aocutils.Context) (int64, error) { +func Day6Part1(c aoc.Context) (int64, error) { c.Logger = log.New(os.Stdout, "", log.LstdFlags) lines, tokens, err := day6ParseInputs(c) if err != nil { @@ -39,7 +39,7 @@ func Day6Part1(c aocutils.Context) (int64, error) { return total, err } -func Day6Part2(c aocutils.Context) (int64, error) { +func Day6Part2(c aoc.Context) (int64, error) { lines, tokens, err := day6ParseInputs(c) if err != nil { return 0, fmt.Errorf("error parsing inputs: %w", err) @@ -110,7 +110,7 @@ func (t day6Token) End() int { return t.Start + t.Size } -func day6ParseInputs(c aocutils.Context) (lines [][]byte, tokens []day6Token, err error) { +func day6ParseInputs(c aoc.Context) (lines [][]byte, tokens []day6Token, err error) { s := c.Scanner() // handle malformed input diff --git a/year25/day7.go b/year25/day7.go index f7a74b0..366b232 100644 --- a/year25/day7.go +++ b/year25/day7.go @@ -8,7 +8,7 @@ import ( "git.bizdoc.ro/private/devkit.git/collections/geometry/v2" ) -func day7ParseInput(c aocutils.Context) (g geometry.Grid[byte], start geometry.Point, _ error) { +func day7ParseInput(c aoc.Context) (g geometry.Grid[byte], start geometry.Point, _ error) { g, err := geometry.ReaderToByteGrid(c.Body) if err != nil { return g, start, fmt.Errorf("day7: failed to read input %w", err) @@ -25,7 +25,7 @@ func day7ParseInput(c aocutils.Context) (g geometry.Grid[byte], start geometry.P return g, start, fmt.Errorf("day7: failed to find start point") } -func Day7Part1(c aocutils.Context) (int, error) { +func Day7Part1(c aoc.Context) (int, error) { g, start, err := day7ParseInput(c) if err != nil { return 0, fmt.Errorf("day7: failed to parse input %w", err) @@ -49,7 +49,7 @@ func Day7Part1(c aocutils.Context) (int, error) { return fill(g, start), nil } -func Day7Part2(c aocutils.Context) (int, error) { +func Day7Part2(c aoc.Context) (int, error) { g, start, err := day7ParseInput(c) if err != nil { return 0, fmt.Errorf("day7: failed to parse input %w", err) @@ -79,7 +79,7 @@ func Day7Part2(c aocutils.Context) (int, error) { return fill(g, h, start), nil } -func Day7Part1Render(c aocutils.Context) (int, error) { +func Day7Part1Render(c aoc.Context) (int, error) { g, start, err := day7ParseInput(c) if err != nil { return 0, fmt.Errorf("day7: failed to parse input %w", err)