package aoc import "slices" type Set map[string]bool func NewSet(l int) Set { return make(map[string]bool, l) } func NewSetFromValues(v ...string) Set { s := NewSet(len(v)) for _, val := range v { s[val] = true } return s } func (s Set) Add(nm string) { s[nm] = true } func (s Set) Remove(nm string) { delete(s, nm) } func (s Set) Contains(nm string) bool { _, ok := s[nm] return ok } func (s Set) Empty() bool { return len(s) == 0 } func (s Set) Size() int { return len(s) } func (s Set) Intersection(o Set) Set { smallest, toCheck := s, o if o.Size() < s.Size() { smallest, toCheck = o, s } ix := NewSet(smallest.Size()) for nm := range smallest { if toCheck.Contains(nm) { ix.Add(nm) } } return ix } func (s Set) Union(o Set) Set { un := NewSet(s.Size() + o.Size()) for nm := range s { un.Add(nm) } for nm := range o { un.Add(nm) } return un } func (s Set) ToSortedSlice() []string { sl := make([]string, 0, s.Size()) for nm := range s { sl = append(sl, nm) } slices.Sort(sl) return sl }