aoc2023/day-15/two.go

47 lines
897 B
Go
Raw Permalink Normal View History

2023-12-15 17:19:33 +00:00
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 {
2023-12-15 18:06:56 +00:00
boxes[boxId] = orderedmap.NewOrderedMap[string, int]()
2023-12-15 17:19:33 +00:00
}
if strings.Contains(lense, "=") {
2023-12-15 19:51:15 +00:00
boxes[boxId].Set(label, focalLength)
2023-12-15 17:19:33 +00:00
} else {
2023-12-16 00:21:45 +00:00
boxes[boxId].Delete(label)
2023-12-15 17:19:33 +00:00
}
}
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
}