#![allow(non_snake_case)] use std::fs::File; use std::io::Read; fn get_opts(t: u64, d: u64) -> u64 { let t = t as f64; let d = d as f64; let D = f64::sqrt((t * t - 4. * d) as f64); let x1 = ((-t - D) / 2.).ceil(); let x2 = ((-t + D) / 2.).floor(); (x2 - x1 + 1.) as u64 } fn main() { // let filename = "in/day06.ref"; let filename = "in/day06.pzl"; let mut f = File::open(filename).expect("cannot open file"); let mut content = String::new(); f.read_to_string(&mut content).expect("cannot read file"); let mut lines = content.trim_end().split('\n'); let parse1 = |l: &str| { l.split_once(":") .unwrap() .1 .split(" ") .filter_map(|x| x.parse::().ok()) .collect::>() }; let time = parse1(lines.next().unwrap()); let dist = parse1(lines.next().unwrap()); let res1 = time .into_iter() .zip(dist.into_iter()) .map(|(t, d)| get_opts(t, d)) .fold(1, |acc, x| acc * x); let mut lines = content.trim_end().split('\n'); let parse2 = |l: &str| { l.split_once(":") .unwrap() .1 .split_whitespace() .collect::>() .join("") .parse() .unwrap() }; let time = parse2(lines.next().unwrap()); let dist = parse2(lines.next().unwrap()); let res2 = get_opts(time, dist); println!("res1: {}", res1); println!("res2: {}", res2); assert_eq!(res1, 220320); assert_eq!(res2, 34454850); }