First optimizations

This gets runtime down to about a quarter of what it was. I need to see if I can apply bisect to the X value, but I'm not sure I understand how/if that can work.
This commit is contained in:
2022-06-21 01:16:42 -05:00
parent 5247ae9d72
commit de07c7c987

View File

@ -89,34 +89,34 @@ func (d *Day19) Part2() string {
}
}
done := false
for y := startY; !done; y++ {
lastGoodX := 0
threshold := 1
y := u.Bisect(startY+100, 9999, threshold, func(y int) bool {
foundX := false
for x := startX; ; x++ {
if !f(x, y) {
if !foundX {
continue
} else {
break
return true
}
}
if !foundX {
foundX = true
startX = x
}
if !f(x+99, y) {
break
return true
}
if !f(x, y+99) {
continue
}
fmt.Printf("x=%d, y=%d, so result=%d\n", x, y, (x*10000)+y)
done = true
break
lastGoodX = x
return false
}
}
})
return "0"
result := (lastGoodX * 10000) + y
return fmt.Sprintf("Closest 100x100 square for the ship starts at %d,%d = %s%d%s", lastGoodX, y, u.TextBold, result, u.TextReset)
}