From de07c7c9877d4052a16a4147bb258e048ed11427 Mon Sep 17 00:00:00 2001 From: Parnic Date: Tue, 21 Jun 2022 01:16:42 -0500 Subject: [PATCH] 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. --- days/19.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/days/19.go b/days/19.go index a11e863..e7b6a09 100644 --- a/days/19.go +++ b/days/19.go @@ -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) }