package day5 import ( "fmt" "math" "strings" "gitea.paas.celticinfo.fr/oabrivard/aoc2023/utils" ) type SrcToDest struct { src int64 dst int64 rng int64 } func buildMapList(start int, lines []string) ([]*SrcToDest, int) { result := []*SrcToDest{} for i := start + 1; len(strings.TrimSpace(lines[i])) > 0; i++ { vals := utils.ParseInt64Array(lines[i], " ") result = append(result, &SrcToDest{ src: vals[1], dst: vals[0], rng: vals[2], }) } /* sort.SliceStable(result, func(i, j int) bool { return result[i].src < result[j].src }) */ return result, start + len(result) + 2 } func srcToDest(src int64, stdList []*SrcToDest) int64 { for _, std := range stdList { if std.src <= src && src <= std.src+std.rng { return src + std.dst - std.src } } return src } func FindClosestLocation(lines []string) int64 { seeds := utils.ParseInt64Array(strings.TrimPrefix(lines[0], "seeds: "), " ") seedToSoil, i := buildMapList(2, lines) soilToFert, i := buildMapList(i, lines) fertToWater, i := buildMapList(i, lines) waterToLight, i := buildMapList(i, lines) lightToTemp, i := buildMapList(i, lines) tempToHumid, i := buildMapList(i, lines) humidToLoc, _ := buildMapList(i, lines) result := int64(math.MaxInt64) for _, seed := range seeds { d1 := srcToDest(seed, seedToSoil) d2 := srcToDest(d1, soilToFert) d3 := srcToDest(d2, fertToWater) d4 := srcToDest(d3, waterToLight) d5 := srcToDest(d4, lightToTemp) d6 := srcToDest(d5, tempToHumid) d7 := srcToDest(d6, humidToLoc) if d7 < result { result = d7 } } return result } func FindRangedClosestLocation(lines []string) int64 { seeds := utils.ParseInt64Array(strings.TrimPrefix(lines[0], "seeds: "), " ") seedToSoil, i := buildMapList(2, lines) soilToFert, i := buildMapList(i, lines) fertToWater, i := buildMapList(i, lines) waterToLight, i := buildMapList(i, lines) lightToTemp, i := buildMapList(i, lines) tempToHumid, i := buildMapList(i, lines) humidToLoc, _ := buildMapList(i, lines) result := int64(math.MaxInt64) count := 0 for i := 0; i < len(seeds); i += 2 { count++ for seed := seeds[i]; seed < seeds[i]+seeds[i+1]; seed++ { d1 := srcToDest(seed, seedToSoil) d2 := srcToDest(d1, soilToFert) d3 := srcToDest(d2, fertToWater) d4 := srcToDest(d3, waterToLight) d5 := srcToDest(d4, lightToTemp) d6 := srcToDest(d5, tempToHumid) d7 := srcToDest(d6, humidToLoc) if d7 < result { result = d7 } } fmt.Println("seed roots done:", count) } return result }