summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGard Spreemann <gspr@nonempty.org>2023-12-06 10:18:53 +0100
committerGard Spreemann <gspr@nonempty.org>2023-12-06 10:18:53 +0100
commit7327953575d79ea99d5b3bc20676c0281baa4b56 (patch)
tree4354d109b2b4f63460220d1c5b13a36841d730fd
parent2f07ac026fea9920f9e2a53a53aa5e652e8fe201 (diff)
Handle the unseen case of no wins
-rw-r--r--06/src/stuff.rs27
1 files changed, 15 insertions, 12 deletions
diff --git a/06/src/stuff.rs b/06/src/stuff.rs
index b3651ab..aa1d53d 100644
--- a/06/src/stuff.rs
+++ b/06/src/stuff.rs
@@ -20,26 +20,29 @@ fn distance(c: u64, n: u64) -> u64 {
n.saturating_sub(c)*c
}
-pub fn num_solutions(time: u64, record: u64) -> u64 {
- //println!("Time avail: {}. Record: {}.", time, record);
-
+fn winning_interval(time: u64, record: u64) -> Option<(u64, u64)> {
let (est_l, est_u) = root_estimate(time, record);
- //println!("Estimate: [{}, {}]", est_l, est_u);
-
+
// Find highest charge time that does not beat the record, looking downwards from lower estimate.
let l0 = (0..=est_l).rev().into_iter().find(|& charge| distance(charge, time) <= record).unwrap_or(0);
// Find lowest charge time that does beat the record, looking upwards from the above.
- let l1 = (l0..=est_u).into_iter().find(|& charge| distance(charge, time) > record).unwrap_or(l0);
-
+ let l1 = (l0..=est_u).into_iter().find(|& charge| distance(charge, time) > record)?;
+
// Find lowest charge time that does not beat the record, looking upwards from upper estimate.
let u0 = (est_u..).into_iter().find(|& charge| distance(charge, time) <= record).unwrap_or(time);
// Find the highest charge time that does beat the record, looking downwards from the above.
- let u1 = (l1..=u0).rev().into_iter().find(|& charge| distance(charge, time) > record).unwrap_or(l1);
-
- //println!("Winning range: [{}, {}]", l1, u1);
- //println!("....");
+ let u1 = (l1..=u0).rev().into_iter().find(|& charge| distance(charge, time) > record)?;
- u1.saturating_sub(l1) + 1
+ Some((l1, u1))
+}
+
+pub fn num_solutions(time: u64, record: u64) -> u64 {
+ if let Some((l, u)) = winning_interval(time, record) {
+ (u - l) + 1
+ }
+ else {
+ 0
+ }
}
pub fn ascii_to_u64(s: & [u8]) -> u64 {