aoc2023/day-5/two.go

79 lines
1.3 KiB
Go
Raw Normal View History

2023-12-06 04:01:17 +00:00
package main
import (
"fmt"
"math"
"sync"
"sync/atomic"
"time"
log "github.com/sirupsen/logrus"
)
type WaitGroupCount struct {
sync.WaitGroup
count int64
}
func (wg *WaitGroupCount) Add(delta int) {
atomic.AddInt64(&wg.count, int64(delta))
wg.WaitGroup.Add(delta)
}
func (wg *WaitGroupCount) Done() {
atomic.AddInt64(&wg.count, -1)
wg.WaitGroup.Done()
}
func (wg *WaitGroupCount) GetCount() int {
return int(atomic.LoadInt64(&wg.count))
}
var wg = WaitGroupCount{}
//var checked = make([]bound, 0)
type bound struct {
start int
end int
}
var bounds = []bound{}
// two returns the lowest location
func two(lines []string) (int, error) {
lowest := math.MaxInt
for i := 0; i < len(seeds); i += 2 {
bounds = append(bounds, bound{start: seeds[i], end: seeds[i] + seeds[i+1]})
}
startTime := time.Now()
go func() {
for {
elapsed := time.Since(startTime)
fmt.Printf("[%s] wg count: %d\n", elapsed.Round(time.Second), wg.GetCount())
time.Sleep(time.Second)
}
}()
for _, bound := range bounds {
wg.Add(1)
go func(start int, end int) {
defer wg.Done()
for i := start; i < end; i++ {
location := next(0, i)
if location < lowest {
lowest = location
}
}
}(bound.start, bound.end)
log.Info(bound, " completed")
}
wg.Wait()
return lowest - 1, nil // returning a value one too high? not sure why.
}