From 4798596607624bc1f81113d275bba6ed5f479294 Mon Sep 17 00:00:00 2001 From: Dylan Butler Date: Sat, 2 Dec 2023 22:38:35 +1100 Subject: [PATCH] 2023-02 sped --- 2023/02/01.go | 4 +++ 2023/02/01v2.go | 68 ++++++++++++++++++++++++++++++++++++++ 2023/02/02.go | 4 +++ 2023/02/02v2.go | 65 ++++++++++++++++++++++++++++++++++++ pkg/benchmark/benchmark.go | 10 +++--- 5 files changed, 146 insertions(+), 5 deletions(-) create mode 100644 2023/02/01v2.go create mode 100644 2023/02/02v2.go diff --git a/2023/02/01.go b/2023/02/01.go index cd09c0d..abc942c 100644 --- a/2023/02/01.go +++ b/2023/02/01.go @@ -4,6 +4,7 @@ import ( "embed" _ "embed" "fmt" + "github.com/dbut2/advent-of-code/pkg/benchmark" "github.com/dbut2/advent-of-code/pkg/sti" "strings" @@ -21,6 +22,9 @@ func main() { t := test.Register(tests, solve) t.Expect(1, 8) fmt.Println(solve(input)) + benchmark.Run(func() { + solve(input) + }, benchmark.Count(1000)) } func solve(input string) int { diff --git a/2023/02/01v2.go b/2023/02/01v2.go new file mode 100644 index 0000000..5bfaa02 --- /dev/null +++ b/2023/02/01v2.go @@ -0,0 +1,68 @@ +package main + +import ( + "embed" + _ "embed" + "fmt" + "github.com/dbut2/advent-of-code/pkg/benchmark" + "github.com/dbut2/advent-of-code/pkg/test" + "github.com/dbut2/advent-of-code/pkg/utils" + "time" +) + +//go:embed input.txt +var input string + +//go:embed test*.txt +var tests embed.FS + +func main() { + t := test.Register(tests, solve) + t.Expect(1, 8) + fmt.Println(solve(input)) + benchmark.Run(func() { + solve(input) + }, benchmark.Time(time.Second)) +} + +func solve(input string) int { + s := utils.ParseInput(input) + + total := 0 + var bufferCount int + var i, j int + var line string + + for i, line = range s { + for j = range line { + if line[j] >= '0' && line[j] <= '9' { + bufferCount *= 10 + bufferCount += int(line[j] - '0') + continue + } + if line[j] == ' ' { + continue + } + switch line[j] { + case 'r': + if bufferCount > 12 { + goto exit + } + case 'g': + if bufferCount > 13 { + goto exit + } + case 'b': + if bufferCount > 14 { + goto exit + } + } + + bufferCount = 0 + } + total += i + 1 + exit: + } + + return total +} diff --git a/2023/02/02.go b/2023/02/02.go index 51e3aff..809db20 100644 --- a/2023/02/02.go +++ b/2023/02/02.go @@ -4,6 +4,7 @@ import ( "embed" _ "embed" "fmt" + "github.com/dbut2/advent-of-code/pkg/benchmark" "github.com/dbut2/advent-of-code/pkg/sti" "strings" @@ -21,6 +22,9 @@ func main() { t := test.Register(tests, solve) t.Expect(2, 2286) fmt.Println(solve(input)) + benchmark.Run(func() { + solve(input) + }, benchmark.Count(1000)) } func solve(input string) int { diff --git a/2023/02/02v2.go b/2023/02/02v2.go new file mode 100644 index 0000000..6578948 --- /dev/null +++ b/2023/02/02v2.go @@ -0,0 +1,65 @@ +package main + +import ( + "embed" + _ "embed" + "fmt" + "github.com/dbut2/advent-of-code/pkg/benchmark" + "github.com/dbut2/advent-of-code/pkg/test" + "github.com/dbut2/advent-of-code/pkg/utils" +) + +//go:embed input.txt +var input string + +//go:embed test*.txt +var tests embed.FS + +func main() { + t := test.Register(tests, solve) + t.Expect(2, 2286) + fmt.Println(solve(input)) + benchmark.Run(func() { + solve(input) + }, benchmark.Count(1000)) +} + +func solve(input string) int { + s := utils.ParseInput(input) + + total := 0 + var bufferCount int + var j int + var line string + + var redCount, greenCount, blueCount int + + for _, line = range s { + redCount, greenCount, blueCount = 0, 0, 0 + + for j = range line { + if line[j] >= '0' && line[j] <= '9' { + bufferCount *= 10 + bufferCount += int(line[j] - '0') + continue + } + if line[j] == ' ' { + continue + } + switch line[j] { + case 'r': + redCount = max(redCount, bufferCount) + case 'g': + greenCount = max(greenCount, bufferCount) + case 'b': + blueCount = max(blueCount, bufferCount) + } + + bufferCount = 0 + } + + total += redCount * greenCount * blueCount + } + + return total +} diff --git a/pkg/benchmark/benchmark.go b/pkg/benchmark/benchmark.go index a211601..67459cf 100644 --- a/pkg/benchmark/benchmark.go +++ b/pkg/benchmark/benchmark.go @@ -3,9 +3,9 @@ package benchmark import ( "fmt" "runtime" + "slices" "time" - "github.com/dbut2/advent-of-code/pkg/math" "github.com/dbut2/advent-of-code/pkg/timer" ) @@ -30,17 +30,17 @@ func Run(f func(), cond Condition) { took := time.Since(start) - ot := math.Order(times, false) + slices.Sort(times) fmt.Println(len(times), "TRIALS IN", took) - printTable(ot) + printTable(times) i := 0 for point, times := range pings { i++ - ot := math.Order(times, false) + slices.Sort(times) fmt.Println() fmt.Println("POINT:", i) - printTable(ot) + printTable(times) fmt.Println(point) } }