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:
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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user