Completed part 2 of Day 5 puzzle

main
oabrivard 2 years ago
parent 7cd872458b
commit 013d806543

@ -4,6 +4,7 @@ import (
"fmt"
"math"
"strings"
"sync"
"gitea.paas.celticinfo.fr/oabrivard/aoc2023/utils"
)
@ -38,7 +39,7 @@ func buildMapList(start int, lines []string) ([]*SrcToDest, int) {
func srcToDest(src int64, stdList []*SrcToDest) int64 {
for _, std := range stdList {
if std.src <= src && src <= std.src+std.rng {
if std.src <= src && src <= std.src+std.rng-1 {
return src + std.dst - std.src
}
}
@ -87,12 +88,17 @@ func FindRangedClosestLocation(lines []string) int64 {
tempToHumid, i := buildMapList(i, lines)
humidToLoc, _ := buildMapList(i, lines)
result := int64(math.MaxInt64)
var wg sync.WaitGroup
ch := make(chan int64, len(seeds)/2)
count := 0
for i := 0; i < len(seeds); i += 2 {
count++
for seed := seeds[i]; seed < seeds[i]+seeds[i+1]; seed++ {
wg.Add(1)
go func(s int64, r int64) {
defer wg.Done()
result := int64(math.MaxInt64)
for seed := s; seed < s+r; seed++ {
d1 := srcToDest(seed, seedToSoil)
d2 := srcToDest(d1, soilToFert)
d3 := srcToDest(d2, fertToWater)
@ -105,7 +111,20 @@ func FindRangedClosestLocation(lines []string) int64 {
result = d7
}
}
fmt.Println("seed roots done:", count)
fmt.Println("seed roots done:", result)
ch <- result
}(seeds[i], seeds[i+1])
}
wg.Wait()
close(ch)
result := int64(math.MaxInt64)
for i := range ch {
if i < result {
result = i
}
}
return result
}

@ -109,7 +109,7 @@ func TestRangedFindClosestLocationWithInput(t *testing.T) {
lines := utils.ReadLines("input.txt")
result := FindRangedClosestLocation(lines)
if result != 1181555926 {
t.Fatalf("expected 37806487, got %d", result)
if result != 37806486 {
t.Fatalf("expected 37806486, got %d", result)
}
}

Loading…
Cancel
Save