mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2026-04-09 02:23:36 +00:00
add day-21 + benchmarks
This commit is contained in:
6
day-21/internal/one/benchmark
Normal file
6
day-21/internal/one/benchmark
Normal file
@@ -0,0 +1,6 @@
|
||||
goos: linux
|
||||
goarch: amd64
|
||||
pkg: github.com/onyx-and-iris/aoc2024/day-21/internal/one
|
||||
cpu: Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
|
||||
BenchmarkSolve-12 1000000000 0.0000606 ns/op
|
||||
ok github.com/onyx-and-iris/aoc2024/day-21/internal/one 0.008s
|
||||
36
day-21/internal/one/solve.go
Normal file
36
day-21/internal/one/solve.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package one
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
||||
"github.com/onyx-and-iris/aoc2024/day-21/internal/pad"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func Solve(buf []byte) (int, error) {
|
||||
r := bytes.NewReader(buf)
|
||||
codes, err := parseLines(r)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
numpad := pad.NewNumpad()
|
||||
dirpad := pad.NewDirpad()
|
||||
|
||||
var complexity int
|
||||
for _, code := range codes {
|
||||
moves := generateMoves(numpad, dirpad, code)
|
||||
complexity += len(moves) * numFromCode(code)
|
||||
log.Debugf("%s\n%d * %d\n", moves, len(moves), numFromCode(code))
|
||||
}
|
||||
|
||||
return complexity, nil
|
||||
}
|
||||
|
||||
type generator interface {
|
||||
Generate(code string) string
|
||||
}
|
||||
|
||||
func generateMoves(n, d generator, code string) string {
|
||||
return d.Generate(d.Generate(n.Generate(code)))
|
||||
}
|
||||
15
day-21/internal/one/solve_internal_test.go
Normal file
15
day-21/internal/one/solve_internal_test.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package one
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
//go:embed testdata/input.txt
|
||||
var data []byte
|
||||
|
||||
func BenchmarkSolve(b *testing.B) {
|
||||
os.Stdout, _ = os.Open(os.DevNull)
|
||||
Solve(data)
|
||||
}
|
||||
27
day-21/internal/one/util.go
Normal file
27
day-21/internal/one/util.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package one
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"io"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func parseLines(r io.Reader) ([]string, error) {
|
||||
codes := []string{}
|
||||
|
||||
scanner := bufio.NewScanner(r)
|
||||
for scanner.Scan() {
|
||||
codes = append(codes, scanner.Text())
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return codes, nil
|
||||
}
|
||||
|
||||
func numFromCode(s string) int {
|
||||
n, _ := strconv.Atoi(s[:len(s)-1])
|
||||
return n
|
||||
}
|
||||
Reference in New Issue
Block a user