diff --git a/days/14.go b/days/14.go index 8b82b17..87b5224 100644 --- a/days/14.go +++ b/days/14.go @@ -104,10 +104,26 @@ func (d *Day14) Part1() string { func (d *Day14) Part2() string { oreAvailable := int64(1000000000000) estimate := oreAvailable / d.getOreRequiredForFuel(1) - lastSuccess := u.Bisect(estimate, estimate*2, 1, func(val int64) bool { - oreConsumed := d.getOreRequiredForFuel(val) - return oreConsumed < oreAvailable - }) + + high := estimate * 2 + low := estimate + + lastSuccess := low + lastFailure := high + fuelProduced := low + + for math.Abs(float64(lastFailure-lastSuccess)) > 1 { + oreConsumed := d.getOreRequiredForFuel(fuelProduced) + adjustment := (lastFailure - lastSuccess) / 2 + if oreConsumed < oreAvailable { + lastSuccess = fuelProduced + } else { + lastFailure = fuelProduced + adjustment = -adjustment + } + + fuelProduced += adjustment + } return fmt.Sprintf("Maximum fuel we can make from 1 trillion ore: %s%d%s", u.TextBold, lastSuccess, u.TextReset) } diff --git a/utilities/bisect.go b/utilities/bisect.go deleted file mode 100644 index 702b9ae..0000000 --- a/utilities/bisect.go +++ /dev/null @@ -1,31 +0,0 @@ -package utilities - -import "math" - -// Bisect takes a known-good low and known-bad high value as the bounds -// to bisect, and a function to test each value for success or failure. -// If the function succeeds, the value is adjusted toward the maximum, -// and if the function fails, the value is adjusted toward the minimum. -// The final value is returned when the difference between the success -// and the failure is less than or equal to the acceptance threshold -// (usually 1, for integers). -func Bisect[T Number](low, high, threshold T, tryFunc func(val T) bool) T { - lastSuccess := low - lastFailure := high - currVal := low - - for T(math.Abs(float64(lastFailure-lastSuccess))) > threshold { - success := tryFunc(currVal) - adjustment := (lastFailure - lastSuccess) / 2 - if success { - lastSuccess = currVal - } else { - lastFailure = currVal - adjustment = -adjustment - } - - currVal += adjustment - } - - return currVal -} diff --git a/utilities/intcode.go b/utilities/intcode.go index d037711..f0bf4b4 100644 --- a/utilities/intcode.go +++ b/utilities/intcode.go @@ -128,15 +128,9 @@ func (p *IntcodeProgram) ensureMemoryCapacity(address int) { } func (p *IntcodeProgram) Reset() { - wiped := false - if len(p.memory) != len(p.program) { - wiped = true - p.memory = nil - } + p.memory = nil p.init() - if !wiped { - copy(p.memory, p.program) - } + copy(p.memory, p.program) p.relativeBase = 0 }