mirror of
https://github.com/onyx-and-iris/aoc2023.git
synced 2024-11-15 15:10:49 +00:00
31 lines
518 B
Go
31 lines
518 B
Go
|
package main
|
||
|
|
||
|
// hasher defines the methods required to hash a string
|
||
|
type hasher struct {
|
||
|
next int
|
||
|
}
|
||
|
|
||
|
// newHasher returns a hasher type
|
||
|
func newHasher() hasher {
|
||
|
return hasher{}
|
||
|
}
|
||
|
|
||
|
// run processes each step of the hasher for each char in a string
|
||
|
func (h hasher) run(in string) int {
|
||
|
hash := 0
|
||
|
for _, r := range in {
|
||
|
h.next = hash + int(r)
|
||
|
hash = h.multiply().modulus()
|
||
|
}
|
||
|
return hash
|
||
|
}
|
||
|
|
||
|
func (h hasher) multiply() hasher {
|
||
|
h.next *= 17
|
||
|
return h
|
||
|
}
|
||
|
|
||
|
func (h hasher) modulus() int {
|
||
|
return h.next % 256
|
||
|
}
|