Skip to content

Commit

Permalink
2023-02 sped
Browse files Browse the repository at this point in the history
  • Loading branch information
dbut2 committed Dec 2, 2023
1 parent c5a65f4 commit 4798596
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 5 deletions.
4 changes: 4 additions & 0 deletions 2023/02/01.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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 {
Expand Down
68 changes: 68 additions & 0 deletions 2023/02/01v2.go
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 4 additions & 0 deletions 2023/02/02.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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 {
Expand Down
65 changes: 65 additions & 0 deletions 2023/02/02v2.go
Original file line number Diff line number Diff line change
@@ -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
}
10 changes: 5 additions & 5 deletions pkg/benchmark/benchmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)
}
}
Expand Down

0 comments on commit 4798596

Please sign in to comment.