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,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
}

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