add day-22 + benchmarks

This commit is contained in:
2024-12-22 16:54:03 +00:00
parent 5cc9a43723
commit 287a168293
19 changed files with 426 additions and 0 deletions

View File

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

View File

@@ -0,0 +1,29 @@
package one
import (
"bytes"
"github.com/onyx-and-iris/aoc2024/day-22/internal/randomiser"
)
const numIterations = 2000
func Solve(buf []byte) (int, error) {
r := bytes.NewReader(buf)
secrets, err := parseLines(r)
if err != nil {
return 0, err
}
var sum int
for _, secret := range secrets {
randomiser := randomiser.New(secret)
for range numIterations {
randomiser.Randomise()
}
sum += randomiser.Secret()
}
return sum, nil
}

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

View File

@@ -0,0 +1,31 @@
package one
import (
"bufio"
"io"
"strconv"
"strings"
)
func parseLines(r io.Reader) ([]int, error) {
secrets := []int{}
scanner := bufio.NewScanner(r)
for scanner.Scan() {
secrets = append(secrets, mustConv(strings.TrimSpace(scanner.Text())))
}
if err := scanner.Err(); err != nil {
return nil, err
}
return secrets, nil
}
func mustConv(s string) int {
n, err := strconv.Atoi(s)
if err != nil {
panic(err)
}
return n
}