add daythree + benchmarks

This commit is contained in:
2024-12-03 14:42:28 +00:00
parent 64f4df4fea
commit f0bf82c96a
14 changed files with 309 additions and 0 deletions

View File

@@ -0,0 +1,6 @@
goos: linux
goarch: amd64
pkg: github.com/onyx-and-iris/aoc2024/day-03/internal/one
cpu: Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
BenchmarkMain-12 1000000000 0.0006976 ns/op
ok github.com/onyx-and-iris/aoc2024/day-03/internal/one 0.010s

View File

@@ -0,0 +1,41 @@
package one
import (
"bufio"
"bytes"
"io"
"regexp"
"github.com/onyx-and-iris/aoc2024/day-03/internal/util"
)
var reMul = regexp.MustCompile(`mul\((?P<first>[0-9]{1,3}),(?P<second>[0-9]{1,3})\)`)
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
scanner := bufio.NewScanner(r)
for scanner.Scan() {
m := reMul.FindAllString(scanner.Text(), -1)
for _, v := range m {
groups := util.GetGroups(reMul, v)
sum += util.MustConv(groups["first"]) * util.MustConv(groups["second"])
}
}
if err := scanner.Err(); err != nil {
return 0, err
}
return sum, nil
}

View File

@@ -0,0 +1,15 @@
package one
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)
}

View 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

View 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
}

View 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)
}

View File

@@ -0,0 +1,25 @@
package util
import (
"regexp"
"strconv"
)
func GetGroups(r *regexp.Regexp, s string) map[string]string {
groups := make(map[string]string)
names := r.SubexpNames()
for i, res := range r.FindStringSubmatch(s) {
if i != 0 {
groups[names[i]] = res
}
}
return groups
}
func MustConv(s string) int {
n, err := strconv.Atoi(s)
if err != nil {
panic(err)
}
return n
}