mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-01-09 14:20:48 +00:00
44 lines
615 B
Go
44 lines
615 B
Go
|
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
|
||
|
}
|