refactor aoc.utils

This commit is contained in:
Gabriel Bizdoc 2025-12-08 05:18:10 +02:00
parent 7e759f3b8a
commit 4ffe17fe12
No known key found for this signature in database
GPG Key ID: 3F0EDAECA5BE9ED9
8 changed files with 35 additions and 28 deletions

View File

@ -1,4 +1,4 @@
package aocutils
package aoc
import (
"bufio"

View File

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

View File

@ -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() {

View File

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

View File

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

View File

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

View File

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

View File

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