qzazwxsx

Untitled

Dec 6th, 2023
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 1.60 KB | Source Code | 0 0
  1. use crate::{Solution, SolutionPair};
  2.  
  3. ///////////////////////////////////////////////////////////////////////////////
  4. fn amt_ways(a: f64, b: f64) -> u64 {
  5.     let lower_bound: u64 = ((a - (a*a - 4.0*b).sqrt()) / 2.0).floor()      as u64;
  6.     let upper_bound: u64 = ((a + (a*a - 4.0*b).sqrt()) / 2.0 - 1.0).ceil() as u64;
  7.     return upper_bound - lower_bound
  8. }
  9.  
  10. pub fn solve(input: std::str::Lines<'_>) -> SolutionPair {
  11.  
  12.    let mut inp = input.clone();
  13.  
  14.    let elements1: Vec<f64> = inp
  15.        .next()
  16.        .unwrap()
  17.        .split_whitespace()
  18.        .skip(1)
  19.        .map(|s| s
  20.            .parse()
  21.            .unwrap())
  22.        .collect();
  23.    let elements2: Vec<f64> = inp
  24.        .next()
  25.        .unwrap()
  26.        .split_whitespace()
  27.        .skip(1)
  28.        .map(|s| s
  29.            .parse()
  30.            .unwrap())
  31.        .collect();
  32.  
  33.    let pairs: Vec<(f64, f64)> = elements1.clone().into_iter().zip(elements2.clone().into_iter()).collect();
  34.  
  35.    let mut product1: u64 = 1;
  36.  
  37.    for (a, b) in pairs {
  38.        product1 *= amt_ways(a, b);
  39.    }
  40.  
  41.    let sol2: u64 = 0;
  42.  
  43.    // ugly parsing here lol
  44.    let result1: f64 = elements1
  45.        .clone()
  46.        .iter()
  47.        .map(|&num| num.to_string())
  48.        .collect::<String>()
  49.        .parse::<f64>()
  50.        .unwrap() as f64;
  51.    let result2: f64 = elements2
  52.        .clone()
  53.        .iter()
  54.        .map(|&num| num.to_string())
  55.        .collect::<String>()
  56.        .parse::<f64>()
  57.        .unwrap() as f64;
  58.  
  59.    let ans2 = amt_ways(result1, result2);
  60.  
  61.    (Solution::from(product1), Solution::from(ans2))
  62. }
  63.  
Advertisement
Add Comment
Please, Sign In to add comment