mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2026-04-08 18:13:36 +00:00
add daythree + benchmarks
This commit is contained in:
6
day-03/internal/two/benchmark
Normal file
6
day-03/internal/two/benchmark
Normal file
@@ -0,0 +1,6 @@
|
||||
goos: linux
|
||||
goarch: amd64
|
||||
pkg: github.com/onyx-and-iris/aoc2024/day-03/internal/two
|
||||
cpu: Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
|
||||
BenchmarkMain-12 1000000000 0.0006480 ns/op
|
||||
ok github.com/onyx-and-iris/aoc2024/day-03/internal/two 0.010s
|
||||
58
day-03/internal/two/solve.go
Normal file
58
day-03/internal/two/solve.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package two
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"io"
|
||||
"regexp"
|
||||
|
||||
"github.com/onyx-and-iris/aoc2024/day-03/internal/util"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var reMulOrDoOrDont = regexp.MustCompile(`mul\((?P<first>[0-9]{1,3}),(?P<second>[0-9]{1,3})\)|do\(\)|don't\(\)`)
|
||||
|
||||
func Solve(data []byte) (int, error) {
|
||||
r := bytes.NewReader(data)
|
||||
sum, err := parseLines(r)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return sum, nil
|
||||
}
|
||||
|
||||
func parseLines(r io.Reader) (int, error) {
|
||||
var sum int
|
||||
var do bool = true
|
||||
|
||||
scanner := bufio.NewScanner(r)
|
||||
for scanner.Scan() {
|
||||
m := reMulOrDoOrDont.FindAllString(scanner.Text(), -1)
|
||||
for _, v := range m {
|
||||
switch v {
|
||||
case "do()":
|
||||
do = true
|
||||
log.Debug("hit a do")
|
||||
case "don't()":
|
||||
do = false
|
||||
log.Debug("hit a don't")
|
||||
}
|
||||
|
||||
if do {
|
||||
groups := util.GetGroups(reMulOrDoOrDont, v)
|
||||
if groups["first"] != "" && groups["second"] != "" {
|
||||
log.Debugf("%s * %s\n", groups["first"], groups["second"])
|
||||
sum += util.MustConv(groups["first"]) * util.MustConv(groups["second"])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return sum, nil
|
||||
}
|
||||
15
day-03/internal/two/solve_internal_test.go
Normal file
15
day-03/internal/two/solve_internal_test.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package two
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
//go:embed testdata/input.txt
|
||||
var data []byte
|
||||
|
||||
func BenchmarkMain(b *testing.B) {
|
||||
os.Stdout, _ = os.Open(os.DevNull)
|
||||
Solve(data)
|
||||
}
|
||||
Reference in New Issue
Block a user