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:
20
days/19.go
20
days/19.go
@ -89,34 +89,34 @@ func (d *Day19) Part2() string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
done := false
|
lastGoodX := 0
|
||||||
for y := startY; !done; y++ {
|
threshold := 1
|
||||||
|
y := u.Bisect(startY+100, 9999, threshold, func(y int) bool {
|
||||||
foundX := false
|
foundX := false
|
||||||
for x := startX; ; x++ {
|
for x := startX; ; x++ {
|
||||||
if !f(x, y) {
|
if !f(x, y) {
|
||||||
if !foundX {
|
if !foundX {
|
||||||
continue
|
continue
|
||||||
} else {
|
} else {
|
||||||
break
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !foundX {
|
if !foundX {
|
||||||
foundX = true
|
foundX = true
|
||||||
startX = x
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if !f(x+99, y) {
|
if !f(x+99, y) {
|
||||||
break
|
return true
|
||||||
}
|
}
|
||||||
if !f(x, y+99) {
|
if !f(x, y+99) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("x=%d, y=%d, so result=%d\n", x, y, (x*10000)+y)
|
lastGoodX = x
|
||||||
done = true
|
return false
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user