mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-01-10 14:50:46 +00:00
42 lines
704 B
Go
42 lines
704 B
Go
|
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
|
||
|
}
|