aoc2024/day-21/internal/two/cache.go

42 lines
704 B
Go
Raw Permalink Normal View History

2024-12-25 14:45:38 +00:00
package two
import "sync"
type robotCache struct {
mu *sync.RWMutex
data map[string]map[int]int
}
func newRobotCache() robotCache {
return robotCache{
mu: &sync.RWMutex{},
data: make(map[string]map[int]int),
}
}
func (rc *robotCache) read(moves string, id int) (int, bool) {
rc.mu.RLock()
defer rc.mu.RUnlock()
v, ok := rc.data[moves][id]
return v, ok
}
func (rc *robotCache) contains(moves string) bool {
rc.mu.RLock()
defer rc.mu.RUnlock()
_, ok := rc.data[moves]
return ok
}
func (rc *robotCache) insert(moves string, id, val int) {
rc.mu.Lock()
defer rc.mu.Unlock()
if _, ok := rc.data[moves]; !ok {
rc.data[moves] = make(map[int]int)
}
rc.data[moves][id] = val
}