Skip to content

Commit

Permalink
[2023] Day 6: Solve b in constant time
Browse files Browse the repository at this point in the history
  • Loading branch information
connorslade committed Dec 6, 2023
1 parent 90eef8e commit e0a3943
Showing 1 changed file with 4 additions and 10 deletions.
14 changes: 4 additions & 10 deletions aoc_2023/src/day_06.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,10 @@ fn parse_b(input: &str) -> Race {

impl Race {
fn ways_to_win(&self) -> u64 {
let mut out = 0;

for i in 0..self.time {
let distance = i * (self.time - i);
if distance > self.distance {
out += 1;
}
}

out
let a = ((self.time * self.time - 4 * self.distance) as f32).sqrt();
let x1 = ((self.time as f32 - a) / 2.0 + 1.0).floor();
let x2 = ((self.time as f32 + a) / 2.0 - 1.0).ceil();
(x2 - x1 + 1.0) as u64
}
}

Expand Down

0 comments on commit e0a3943

Please sign in to comment.