mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-01-10 06:40:47 +00:00
37 lines
722 B
Go
37 lines
722 B
Go
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)))
|
|
}
|