Skip to content

Commit

Permalink
14b: use binary search from stdlib
Browse files Browse the repository at this point in the history
  • Loading branch information
nlowe committed Dec 14, 2019
1 parent b519b34 commit 08806ab
Showing 1 changed file with 5 additions and 13 deletions.
18 changes: 5 additions & 13 deletions challenge/day14/b.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package day14

import (
"fmt"
"sort"

"github.com/nlowe/aoc2019/challenge"
"github.com/spf13/cobra"
Expand All @@ -22,17 +23,8 @@ func b(challenge *challenge.Input) int {
equations[eqn.output.name] = eqn
}

lo := 1000000000000 / cost(componentFuel, 1, equations, map[string]int{})
hi := 1000000000000

for lo < hi {
mid := (lo + hi + 1) / 2
if cost(componentFuel, mid, equations, map[string]int{}) <= 1000000000000 {
lo = mid
} else {
hi = mid - 1
}
}

return lo
// not sure why this is off by one
return sort.Search(1000000000000, func(i int) bool {
return cost(componentFuel, i, equations, map[string]int{}) >= 1000000000000
}) - 1
}

0 comments on commit 08806ab

Please sign in to comment.