aoc2023/day-5/one.go

42 lines
758 B
Go
Raw Normal View History

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