day6 part 2

This commit is contained in:
Candygoblen123 2023-12-06 10:46:32 -05:00
parent fea1f85aee
commit 5211c5dfe7
No known key found for this signature in database
GPG Key ID: 577DA64EBEF10385

View File

@ -3,28 +3,57 @@ use std::fs;
fn main() { fn main() {
let input = fs::read_to_string("input.txt").unwrap(); let input = fs::read_to_string("input.txt").unwrap();
let input: Vec<_> = input.split('\n') // Seperate the Time and Distance lines let input: Vec<_> = input.split('\n') // Seperate the Time and Distance lines
.map(|line| { .map(|line| {
&line[line.find(':').unwrap() + 1..] // Drop "Time:" and "Distance:" &line[line.find(':').unwrap() + 1..] // Drop "Time:" and "Distance:"
}).map(|line| { }).map(|line| {
line.split_whitespace() // Split the numbers into their own elements. line.split_whitespace().flat_map(|s| s.chars()).collect::<String>() // Split the numbers into their own elements.
.map(|num| num.parse::<i32>().expect("Couldn't parse number")) // Parse numbers into i32 })
.collect::<Vec<_>>() .map(|num| num.parse::<i64>().expect("Couldn't parse number")) // Parse numbers into i32
}).collect(); // collect into Vec .collect(); // collect into Vec
let mut valid_total = 1;
for round in 0..input.first().unwrap().len() { let time = input[0];
let time = input[0][round]; let dist = input[1];
let dist = input[1][round]; let mut valid = 0;
let mut valid = 0;
for remaining_time in 0..time { for remaining_time in 0..time {
if (time - remaining_time) * remaining_time > dist { if (time - remaining_time) * remaining_time > dist {
valid += 1; 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::<i32>().expect("Couldn't parse number")) // Parse numbers into i32
// .collect::<Vec<_>>()
// }).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);
// }