From 5211c5dfe749d25fc108acf91df79e469b9418a2 Mon Sep 17 00:00:00 2001 From: Candygoblen123 Date: Wed, 6 Dec 2023 10:46:32 -0500 Subject: [PATCH] day6 part 2 --- day6/src/main.rs | 65 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 47 insertions(+), 18 deletions(-) diff --git a/day6/src/main.rs b/day6/src/main.rs index 4f7476f..f3f6072 100644 --- a/day6/src/main.rs +++ b/day6/src/main.rs @@ -3,28 +3,57 @@ use std::fs; fn main() { let input = fs::read_to_string("input.txt").unwrap(); let input: Vec<_> = input.split('\n') // Seperate the Time and Distance lines - .map(|line| { - &line[line.find(':').unwrap() + 1..] // Drop "Time:" and "Distance:" - }).map(|line| { - line.split_whitespace() // Split the numbers into their own elements. - .map(|num| num.parse::().expect("Couldn't parse number")) // Parse numbers into i32 - .collect::>() - }).collect(); // collect into Vec + .map(|line| { + &line[line.find(':').unwrap() + 1..] // Drop "Time:" and "Distance:" + }).map(|line| { + line.split_whitespace().flat_map(|s| s.chars()).collect::() // Split the numbers into their own elements. + }) + .map(|num| num.parse::().expect("Couldn't parse number")) // Parse numbers into i32 + .collect(); // collect into Vec - let mut valid_total = 1; - for round in 0..input.first().unwrap().len() { - let time = input[0][round]; - let dist = input[1][round]; - let mut valid = 0; + let time = input[0]; + let dist = input[1]; + let mut valid = 0; - for remaining_time in 0..time { - if (time - remaining_time) * remaining_time > dist { - valid += 1; - } + for remaining_time in 0..time { + if (time - remaining_time) * remaining_time > dist { + valid += 1; } - valid_total *= valid; } - println!("{}", valid_total); + println!("{}", valid); } + + + +// use std::fs; + +// fn main() { +// let input = fs::read_to_string("input.txt").unwrap(); +// let input: Vec<_> = input.split('\n') // Seperate the Time and Distance lines +// .map(|line| { +// &line[line.find(':').unwrap() + 1..] // Drop "Time:" and "Distance:" +// }).map(|line| { +// line.split_whitespace() // Split the numbers into their own elements. +// .map(|num| num.parse::().expect("Couldn't parse number")) // Parse numbers into i32 +// .collect::>() +// }).collect(); // collect into Vec + +// let mut valid_total = 1; + +// for round in 0..input.first().unwrap().len() { +// let time = input[0][round]; +// let dist = input[1][round]; +// let mut valid = 0; + +// for remaining_time in 0..time { +// if (time - remaining_time) * remaining_time > dist { +// valid += 1; +// } +// } +// valid_total *= valid; +// } + +// println!("{}", valid_total); +// }