day5, part2
This commit is contained in:
parent
02abc9d0e8
commit
7dbc09d3f8
|
@ -5,6 +5,7 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"math"
|
"math"
|
||||||
"os"
|
"os"
|
||||||
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
@ -48,7 +49,7 @@ func applyMap(source int, mapping map[int]int) int {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a Almanach)locationForSeed(seed int) int {
|
func (a Almanach)locationForSeed(seed int) int {
|
||||||
log.Print("starting calc of location for seed %d", seed)
|
// log.Print("starting calc of location for seed %n", seed)
|
||||||
soil := a.SeedToSoil.applyMap(seed)
|
soil := a.SeedToSoil.applyMap(seed)
|
||||||
fert := a.SoilToFert.applyMap(soil)
|
fert := a.SoilToFert.applyMap(soil)
|
||||||
water := a.FertToWater.applyMap(fert)
|
water := a.FertToWater.applyMap(fert)
|
||||||
|
@ -105,17 +106,53 @@ func Run() int {
|
||||||
|
|
||||||
result := math.MaxInt
|
result := math.MaxInt
|
||||||
log.Print("before seed range")
|
log.Print("before seed range")
|
||||||
for _, seed := range seeds {
|
seedRanges := make([][]int, 0)
|
||||||
log.Printf("checking for seed %d", seed)
|
for i := 0; i < len(seeds)-1; i += 2 {
|
||||||
location := almanach.locationForSeed(seed)
|
start := seeds[i]
|
||||||
if location < result {
|
seedRangeLen := seeds[i+1]
|
||||||
result = location
|
seedRanges = append(seedRanges, []int{start, start+seedRangeLen})
|
||||||
|
}
|
||||||
|
|
||||||
|
mergedSeedRanges := merge(seedRanges)
|
||||||
|
for _, seedRange := range mergedSeedRanges {
|
||||||
|
log.Printf("starting new range %+v. current min is %n", seedRange, result)
|
||||||
|
for i := seedRange[0]; i <= seedRange[1]; i++ {
|
||||||
|
location := almanach.locationForSeed(i)
|
||||||
|
if location < result {
|
||||||
|
result = location
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// copy from https://codereview.stackexchange.com/questions/259048/merge-intervalsgolang
|
||||||
|
func merge(intervals [][]int) [][]int {
|
||||||
|
const start, end = 0, 1
|
||||||
|
|
||||||
|
var merged [][]int
|
||||||
|
|
||||||
|
if len(intervals) > 1 {
|
||||||
|
sort.Slice(intervals, func(i, j int) bool {
|
||||||
|
return intervals[i][start] < intervals[j][start]
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, interval := range intervals {
|
||||||
|
last := len(merged) - 1
|
||||||
|
if last < 0 || interval[start] > merged[last][end] {
|
||||||
|
merged = append(merged,
|
||||||
|
[]int{start: interval[start], end: interval[end]},
|
||||||
|
)
|
||||||
|
} else if interval[end] > merged[last][end] {
|
||||||
|
merged[last][end] = interval[end]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return merged[:len(merged):len(merged)]
|
||||||
|
}
|
||||||
|
|
||||||
func ReadMap(filename string) EfficientMap {
|
func ReadMap(filename string) EfficientMap {
|
||||||
log.Printf("reading in map from %s", filename)
|
log.Printf("reading in map from %s", filename)
|
||||||
result := make([]Triplet, 0)
|
result := make([]Triplet, 0)
|
||||||
|
|
Loading…
Reference in New Issue