package main import ( "strings" "github.com/elliotchance/orderedmap/v2" ) var boxes = map[int]*orderedmap.OrderedMap[string, int]{} // two returns the sum of all lense configurations func two(lines []string) int { hash := newHasher() for _, lense := range lenses { label, focalLength := func() (string, int) { x := strings.Split(lense, "=") if len(x) == 2 { return x[0], mustConv(x[1]) } return strings.TrimRight(x[0], "-"), 0 }() boxId := hash.run(label) _, ok := boxes[boxId] if !ok { boxes[boxId] = orderedmap.NewOrderedMap[string, int]() } if strings.Contains(lense, "=") { boxes[boxId].Set(label, focalLength) } else { boxes[boxId].Delete(strings.TrimRight(label, "-")) } } sum := 0 for id, box := range boxes { for index, lense := range box.Keys() { focalLength, _ := box.Get(lense) sum += (id + 1) * (index + 1) * focalLength } } return sum }