mirror of
https://github.com/onyx-and-iris/aoc2023.git
synced 2024-11-15 15:10:49 +00:00
79 lines
1.3 KiB
Go
79 lines
1.3 KiB
Go
|
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.
|
||
|
}
|