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