aoc2023/day-5/one.go
2024-01-01 11:32:43 +00:00

42 lines
758 B
Go

package main
import (
"math"
)
var seeds = []int{}
// next recursively calculates each destination for each set of data in dataMap
func next(i int, datapoint int) int {
if i == len(identifiers) {
return datapoint
}
dest := 0
for _, data := range dataMap[identifiers[i]] {
if datapoint >= data.source && datapoint <= data.source+data.offset {
dest = data.dest + (datapoint - data.source)
break
}
}
if dest == 0 {
dest = datapoint
}
return next(i+1, dest)
}
// one returns the lowest location for any seed in seeds
func one(lines []string) (int, error) {
parseLines(lines)
lowest := math.MaxInt
for _, seed := range seeds {
location := next(0, seed)
if location < lowest {
lowest = location
}
}
return lowest, nil
}