Skip to content

Commit 47b39bf

Browse files
authored
Use Hermann Bottenbruch's binary search (#9)
1 parent 5c0fc8c commit 47b39bf

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

src/year2019/day14.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,15 @@ pub fn part1(input: &[Reaction]) -> u64 {
7979
/// Find the maximum amount of fuel possible from 1 trillion ore with an efficient binary search.
8080
pub fn part2(input: &[Reaction]) -> u64 {
8181
let threshold = 1_000_000_000_000;
82-
let mut start = 1;
82+
let mut start = 1_u64;
8383
let mut end = threshold;
8484

85-
while start < end {
86-
let middle = (start + end) / 2;
85+
while start != end {
86+
let middle = (start + end).div_ceil(2);
8787

8888
match ore(input, middle).cmp(&threshold) {
89-
Ordering::Less => start = middle + 1,
90-
Ordering::Equal => return middle,
9189
Ordering::Greater => end = middle - 1,
90+
_ => start = middle,
9291
}
9392
}
9493

0 commit comments

Comments
 (0)