aoc2023/day-15/hasher.go

31 lines
518 B
Go
Raw Permalink Normal View History

2023-12-15 17:19:33 +00:00
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
}