use std::fs; use std::env; use std::iter::zip; fn main() { println!("Hello world!"); let args: Vec = env::args().collect(); if args.len() != 2 { println!("wrong number of arguments!"); std::process::exit(1); } let file_path = &args[1]; // get time lineaswords and distance line as words let binding = fs::read_to_string(file_path) .expect("Should have been able to read the file"); let mut contents = binding.lines(); let times = contents.next() .expect("should have a line of times"); let distances = contents.next() .expect("should have aline of distances"); let pairs = zip( times.split_whitespace(), distances.split_whitespace()).skip(1); // skip the first (label) let mut product = 1; for (time_s, distance_s) in pairs { let time: u128 = time_s.parse().unwrap(); let distance: u128 = distance_s.parse().unwrap(); println!("{} ms, {} meters", time, distance); let winning_strats = win(time, distance); println!("there are {} ways to win", winning_strats); product = product * winning_strats; } println!("product of winning strats is {}", product); let time_s = String::from( times.replace(" ","").rsplit(':').next() .expect("we should have a time") ); let distance_s = String::from( distances.replace(" ","").rsplit(':').next() .expect("we should have a distance") ); println!("the big race is {} milliseconds and {} meters", time_s, distance_s); let time: u128 = time_s.parse().unwrap(); let distance: u128 = distance_s.parse().unwrap(); println!("{} ms, {} meters", time, distance); println!("{} wins", win(time,distance)); } fn win(time: u128, distance: u128) -> u128 { let mut num_wins = 0; for delay in 0..time { if delay * (time - delay) > distance { num_wins = num_wins + 1; } } return num_wins; }