mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2026-04-09 02:23:36 +00:00
add day-22 + benchmarks
This commit is contained in:
6
day-22/internal/one/benchmark
Normal file
6
day-22/internal/one/benchmark
Normal 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
|
||||
29
day-22/internal/one/solve.go
Normal file
29
day-22/internal/one/solve.go
Normal 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
|
||||
}
|
||||
15
day-22/internal/one/solve_internal_test.go
Normal file
15
day-22/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)
|
||||
}
|
||||
31
day-22/internal/one/util.go
Normal file
31
day-22/internal/one/util.go
Normal 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
|
||||
}
|
||||
43
day-22/internal/randomiser/randomiser.go
Normal file
43
day-22/internal/randomiser/randomiser.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package randomiser
|
||||
|
||||
type Randomiser struct {
|
||||
secret int
|
||||
}
|
||||
|
||||
func New(secret int) *Randomiser {
|
||||
return &Randomiser{secret: secret}
|
||||
}
|
||||
|
||||
func (r *Randomiser) multiplyBy(n int) int {
|
||||
return r.secret * n
|
||||
}
|
||||
|
||||
func (r *Randomiser) divideBy(n int) int {
|
||||
return r.secret / n
|
||||
}
|
||||
|
||||
func (r *Randomiser) mix(a int) {
|
||||
r.secret ^= a
|
||||
}
|
||||
|
||||
func (r *Randomiser) prune() {
|
||||
r.secret %= 16777216
|
||||
}
|
||||
|
||||
func (r *Randomiser) Randomise() {
|
||||
res := r.multiplyBy(64)
|
||||
r.mix(res)
|
||||
r.prune()
|
||||
|
||||
res = r.divideBy(32)
|
||||
r.mix(res)
|
||||
r.prune()
|
||||
|
||||
res = r.multiplyBy(2048)
|
||||
r.mix(res)
|
||||
r.prune()
|
||||
}
|
||||
|
||||
func (r *Randomiser) Secret() int {
|
||||
return r.secret
|
||||
}
|
||||
19
day-22/internal/randomiser/randomiser_internal_test.go
Normal file
19
day-22/internal/randomiser/randomiser_internal_test.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package randomiser
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestRandomiserMix(t *testing.T) {
|
||||
r := New(42)
|
||||
r.mix(15)
|
||||
if r.secret != 37 {
|
||||
t.Errorf("r.secret: got = %d want = 3", r.secret)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRandomiserPrune(t *testing.T) {
|
||||
r := New(10e7)
|
||||
r.prune()
|
||||
if r.secret != 16113920 {
|
||||
t.Errorf("r.secret: got = %d want = 16113920", r.secret)
|
||||
}
|
||||
}
|
||||
6
day-22/internal/two/benchmark
Normal file
6
day-22/internal/two/benchmark
Normal file
@@ -0,0 +1,6 @@
|
||||
goos: linux
|
||||
goarch: amd64
|
||||
pkg: github.com/onyx-and-iris/aoc2024/day-22/internal/two
|
||||
cpu: Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
|
||||
BenchmarkSolve-12 1 2064547562 ns/op
|
||||
ok github.com/onyx-and-iris/aoc2024/day-22/internal/two 2.091s
|
||||
21
day-22/internal/two/cache.go
Normal file
21
day-22/internal/two/cache.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package two
|
||||
|
||||
import "sync"
|
||||
|
||||
type sequenceCache struct {
|
||||
mu sync.RWMutex
|
||||
data map[sequence]int
|
||||
}
|
||||
|
||||
func newSequenceCache() sequenceCache {
|
||||
return sequenceCache{
|
||||
data: make(map[sequence]int),
|
||||
}
|
||||
}
|
||||
|
||||
func (sc *sequenceCache) upsert(seq sequence, value int) {
|
||||
sc.mu.Lock()
|
||||
defer sc.mu.Unlock()
|
||||
|
||||
sc.data[seq] += value
|
||||
}
|
||||
7
day-22/internal/two/sequence.go
Normal file
7
day-22/internal/two/sequence.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package two
|
||||
|
||||
type sequence [4]int
|
||||
|
||||
func (s *sequence) shift(next int) {
|
||||
s[0], s[1], s[2], s[3] = s[1], s[2], s[3], next
|
||||
}
|
||||
56
day-22/internal/two/solve.go
Normal file
56
day-22/internal/two/solve.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package two
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"math"
|
||||
|
||||
"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
|
||||
}
|
||||
|
||||
conc := len(secrets)
|
||||
done := make(chan bool)
|
||||
|
||||
sequences := newSequenceCache()
|
||||
for _, secret := range secrets {
|
||||
go func() {
|
||||
seen := make(map[sequence]struct{})
|
||||
sequence := sequence{math.MaxInt, math.MaxInt, math.MaxInt, math.MaxInt}
|
||||
|
||||
randomiser := randomiser.New(secret)
|
||||
for range numIterations {
|
||||
prev := lastDigit(randomiser.Secret())
|
||||
randomiser.Randomise()
|
||||
|
||||
sequence.shift(prev - lastDigit(randomiser.Secret()))
|
||||
|
||||
_, ok := seen[sequence]
|
||||
if !ok {
|
||||
seen[sequence] = struct{}{}
|
||||
sequences.upsert(sequence, lastDigit(randomiser.Secret()))
|
||||
}
|
||||
}
|
||||
done <- true
|
||||
}()
|
||||
}
|
||||
|
||||
for range conc {
|
||||
<-done
|
||||
}
|
||||
|
||||
max := 0
|
||||
for _, sum := range sequences.data {
|
||||
if sum > max {
|
||||
max = sum
|
||||
}
|
||||
}
|
||||
return max, nil
|
||||
}
|
||||
15
day-22/internal/two/solve_internal_test.go
Normal file
15
day-22/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 BenchmarkSolve(b *testing.B) {
|
||||
os.Stdout, _ = os.Open(os.DevNull)
|
||||
Solve(data)
|
||||
}
|
||||
35
day-22/internal/two/util.go
Normal file
35
day-22/internal/two/util.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package two
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func lastDigit(n int) int {
|
||||
return n % 10
|
||||
}
|
||||
Reference in New Issue
Block a user