summaryrefslogblamecommitdiffstats
path: root/2023/rs/code/six/src/main.rs
blob: 35043804a83d64d5f6a5a5bcf6a3f014b7f12ea6 (plain) (tree)



























                                                                                                                      
static INPUT: &str = include_str!("input.txt");

#[inline(always)]
fn exceeding_count(time: u64, dist: u64) -> u64 {
  let fac = (2..time).find(|a| a * (time - a) > dist).unwrap();
  time - fac * 2 + 1
}

fn main() {
  let (times, distances) = {
    let mut lines = INPUT.split('\n').filter(|s| !s.is_empty());
    let times = lines.next().and_then(|l| l.split(":").nth(1)).unwrap();
    let distances = lines.next().and_then(|l| l.split(":").nth(1)).unwrap();
    (times, distances)
  };

  let time_dist = {
    let times = times.split_ascii_whitespace().map(|a| a.parse().unwrap());
    let distances = distances.split_ascii_whitespace().map(|a| a.parse().unwrap());
    times.zip(distances)
  };
  let ways = time_dist.map(|(t,d)| exceeding_count(t, d)).product::<u64>();
  println!("Number of ways to beat past 'winners'(losers): {ways}");

  let (bigtime, bigdistance) = (times.replace(' ', "").parse().unwrap(), distances.replace(' ', "").parse().unwrap());
  let ways = exceeding_count(bigtime, bigdistance);
  println!("Number of ways to beat past (really long race) 'winners'(losers): {ways}");
}