fix day-5 p2

This commit is contained in:
onyx-and-iris 2024-01-01 11:32:43 +00:00
parent 73298dae8d
commit cc452c76a4
6 changed files with 156 additions and 110 deletions

View File

@ -1,19 +1,22 @@
package main package main
type Data struct { type data struct {
dest int dest int
source int source int
offset int offset int
} }
var seeds = []int{} func newData(nums ...int) data {
return data{dest: nums[0], source: nums[1], offset: nums[2]}
var dataMap = map[string][]Data{
"seed-to-soil": make([]Data, 0),
"soil-to-fertilizer": make([]Data, 0),
"fertilizer-to-water": make([]Data, 0),
"water-to-light": make([]Data, 0),
"light-to-temperature": make([]Data, 0),
"temperature-to-humidity": make([]Data, 0),
"humidity-to-location": make([]Data, 0),
} }
func (d data) transform(start, end int) (int, int) {
f := func(x int) int {
return x - d.source + d.dest
}
return f(start), f(end - 1)
}
var dataMap = map[string][]data{}
var identifiers = []string{}

View File

@ -2,21 +2,18 @@ package main
import ( import (
"math" "math"
log "github.com/sirupsen/logrus"
) )
var identifiers = []string{"seed-to-soil", "soil-to-fertilizer", "fertilizer-to-water", "water-to-light", "light-to-temperature", "temperature-to-humidity", "humidity-to-location"} var seeds = []int{}
// next recursively calculates each destination for each set of data in dataMap
func next(i int, datapoint int) int { func next(i int, datapoint int) int {
if i == len(identifiers) { if i == len(identifiers) {
return datapoint return datapoint
} }
dest := func() int {
datas := dataMap[identifiers[i]]
dest := 0 dest := 0
for _, data := range datas { for _, data := range dataMap[identifiers[i]] {
if datapoint >= data.source && datapoint <= data.source+data.offset { if datapoint >= data.source && datapoint <= data.source+data.offset {
dest = data.dest + (datapoint - data.source) dest = data.dest + (datapoint - data.source)
break break
@ -25,20 +22,16 @@ func next(i int, datapoint int) int {
if dest == 0 { if dest == 0 {
dest = datapoint dest = datapoint
} }
return dest
}()
//log.Debug(identifiers[i], ": ", dest)
return next(i+1, dest) return next(i+1, dest)
} }
// one returns the lowest location // one returns the lowest location for any seed in seeds
func one(lines []string) (int, error) { func one(lines []string) (int, error) {
lowest := math.MaxInt
parseLines(lines) parseLines(lines)
lowest := math.MaxInt
for _, seed := range seeds { for _, seed := range seeds {
location := next(0, seed) location := next(0, seed)
log.Info(location)
if location < lowest { if location < lowest {
lowest = location lowest = location
} }

51
day-5/queue.go Normal file
View File

@ -0,0 +1,51 @@
package main
import (
log "github.com/sirupsen/logrus"
)
// queue represents a FIFO queue of bounds
type queue struct {
size int
elements []bound
}
// newQueue returns a queue type
// it initializes the queue size and elements
func newQueue(size int, elems []bound) queue {
return queue{size: size, elements: elems}
}
// enqueue adds an item to the queue
func (q *queue) enqueue(elem bound) {
if q.size >= 0 && q.getLength() == q.size {
log.Info("Queue Overflow")
return
}
q.elements = append(q.elements, elem)
}
// dequeue pops an element from the start of the queue
func (q *queue) dequeue() bound {
if q.isEmpty() {
log.Info("Queue UnderFlow")
return bound{}
}
element := q.elements[0]
if q.getLength() == 1 {
q.elements = nil
return element
}
q.elements = q.elements[1:]
return element
}
// getLength returns the number of items in the queue
func (q *queue) getLength() int {
return len(q.elements)
}
// isEmpty returns true if no items are in the queue
func (q *queue) isEmpty() bool {
return len(q.elements) == 0
}

View File

@ -7,7 +7,7 @@ import (
) )
func init() { func init() {
log.SetLevel(log.DebugLevel) log.SetLevel(log.InfoLevel)
} }
func main() { func main() {
@ -19,9 +19,6 @@ func main() {
} }
fmt.Printf("solution one: %d\n", ans) fmt.Printf("solution one: %d\n", ans)
ans, err = two(lines) ans = two(lines)
if err != nil {
log.Fatal(err)
}
fmt.Printf("solution two: %d\n", ans) fmt.Printf("solution two: %d\n", ans)
} }

View File

@ -1,78 +1,85 @@
package main package main
import ( import (
"fmt"
"math" "math"
"sync" "sync"
"sync/atomic"
"time"
log "github.com/sirupsen/logrus"
) )
type WaitGroupCount struct { var wg sync.WaitGroup
sync.WaitGroup
count int64
}
func (wg *WaitGroupCount) Add(delta int) { const UNLIMITED = -1
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)
// bound represents the lower and upper limits of a single range
type bound struct { type bound struct {
start int lower, upper int
end int
} }
var bounds = []bound{} // newBound returns a bound type
func newBound(lower, upper int) bound {
return bound{lower: lower, upper: upper}
}
// two returns the lowest location // nextTransform recursively calculates each new set of seed ranges for each set of data in dataMap
func two(lines []string) (int, error) { func nextTransform(i int, in []bound) []bound {
lowest := math.MaxInt if i == len(identifiers) {
return in
for i := 0; i < len(seeds); i += 2 {
bounds = append(bounds, bound{start: seeds[i], end: seeds[i] + seeds[i+1]})
} }
startTime := time.Now() q := newQueue(UNLIMITED, in)
in = make([]bound, 0)
for !q.isEmpty() {
r := q.dequeue()
go func() { hasOverlap := func() bool {
for { for _, data := range dataMap[identifiers[i]] {
elapsed := time.Since(startTime) start := max(r.lower, data.source)
fmt.Printf("[%s] wg count: %d\n", elapsed.Round(time.Second), wg.GetCount()) end := min(r.upper, data.source+data.offset)
time.Sleep(time.Second)
if isOverlapping(start, end) {
// add new seed range
in = append(in, newBound(data.transform(start, end)))
// append unmatched portions of seed range back into queue
if start > r.lower {
q.enqueue(newBound(r.lower, start-1))
} }
if r.upper > end {
q.enqueue(newBound(end, r.upper-1))
}
return true
}
}
return false
}() }()
for _, bound := range bounds { // there was no overlap, add the seed range as is
if !hasOverlap {
in = append(in, r)
}
}
return nextTransform(i+1, in)
}
// two returns the lowest location for any seed in seedRanges
func two(lines []string) int {
var seedRanges = []bound{}
for i := 0; i < len(seeds); i += 2 {
wg.Add(1) wg.Add(1)
go func(start int, end int) { go func(i int) {
defer wg.Done() defer wg.Done()
for i := start; i < end; i++ { seedRanges = append(seedRanges, nextTransform(0, []bound{newBound(seeds[i], seeds[i]+seeds[i+1]-1)})...)
location := next(0, i) }(i)
if location < lowest {
lowest = location
}
}
}(bound.start, bound.end)
log.Info(bound, " completed")
} }
wg.Wait() wg.Wait()
return lowest - 1, nil // returning a value one too high? not sure why. lowest := math.MaxInt
for _, r := range seedRanges {
if r.lower < lowest {
lowest = r.lower
}
}
return lowest
} }

View File

@ -2,12 +2,13 @@ package main
import ( import (
"bufio" "bufio"
"log"
"os" "os"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
"unicode" "unicode"
log "github.com/sirupsen/logrus"
) )
// readlines reads lines from stdin. // readlines reads lines from stdin.
@ -27,28 +28,27 @@ func readlines() []string {
return lines return lines
} }
var r = regexp.MustCompile(`([\w-]+) map[:]`)
// parseLines parses input to a data map // parseLines parses input to a data map
func parseLines(lines []string) { func parseLines(lines []string) {
var _regex_identifier = regexp.MustCompile(`(?P<identifier>[\w-]+) map[:]`)
f := func(c rune) bool { f := func(c rune) bool {
return !unicode.IsDigit(c) return !unicode.IsDigit(c)
} }
for i := 0; i < len(lines); i++ { seeds = convertToInts(strings.FieldsFunc(lines[0], f))
if i == 0 {
seeds = convertToInts(strings.FieldsFunc(lines[i], f))
continue
}
m := _regex_identifier.FindStringSubmatch(lines[i]) for i := 2; i < len(lines); i++ {
m := r.FindStringSubmatch(lines[i])
if len(m) == 2 { if len(m) == 2 {
identifiers = append(identifiers, m[1])
for i = i + 1; i < len(lines) && len(lines[i]) != 0; i++ { for i = i + 1; i < len(lines) && len(lines[i]) != 0; i++ {
nums := convertToInts(strings.FieldsFunc(lines[i], f)) d := newData(convertToInts(strings.FieldsFunc(lines[i], f))...)
dataMap[m[1]] = append(dataMap[m[1]], Data{dest: nums[0], source: nums[1], offset: nums[2]}) dataMap[m[1]] = append(dataMap[m[1]], d)
} }
} }
} }
log.Debug(identifiers)
} }
// convertToInts converts a string representing ints to an array of ints // convertToInts converts a string representing ints to an array of ints
@ -61,13 +61,8 @@ func convertToInts(data []string) []int {
return nums return nums
} }
/* // isOverlapping returns true if two ranges overlap. see:
func isChecked(i int) bool { // https://stackoverflow.com/questions/325933/determine-whether-two-date-ranges-overlap/325964#325964
for _, bound := range checked { func isOverlapping(maxStart, minEnd int) bool {
if i > bound.start && i < bound.end { return maxStart < minEnd
return true
}
}
return false
} }
*/