day 6 complete

This commit is contained in:
simon
2024-10-15 16:28:47 +02:00
parent bac0537249
commit 661c1fee19

View File

@@ -7,6 +7,9 @@ fn main() {
let content = fs::read_to_string("input").expect("Could not read the file");
let part1 = part1(content.clone());
println!("Part 1: {}", part1);
let part2 = part2(content);
println!("Part 1: {}", part2);
}
fn part1(content: String) -> i64 {
@@ -18,7 +21,7 @@ fn part1(content: String) -> i64 {
.into_iter()
.filter(|x| !x.is_empty())
.collect::<Vec<_>>();
let distance = content.split("\n").collect::<Vec<_>>()[1]
let distances = content.split("\n").collect::<Vec<_>>()[1]
.split(":")
.collect::<Vec<_>>()[1]
.split(" ")
@@ -26,12 +29,35 @@ fn part1(content: String) -> i64 {
.into_iter()
.filter(|x| !x.is_empty())
.collect::<Vec<_>>();
let races = c![(times[i].parse::<i64>().unwrap(), distance[i].parse::<i64>().unwrap()), for i in 0..times.len()];
let races = c![(times[i].parse::<i64>().unwrap(), distances[i].parse::<i64>().unwrap()), for i in 0..times.len()];
let result = c![get_possible_answers(x.0,x.1), for x in races].into_iter().product::<i64>();
return result;
}
fn part2(content: String) -> i64 {
let binding = content.split("\n").collect::<Vec<_>>()[0]
.split(":")
.collect::<Vec<_>>()[1]
.replace(" ", "");
let times= binding
.split(" ")
.collect::<Vec<_>>()
.into_iter()
.filter(|x| !x.is_empty())
.collect::<Vec<_>>()[0];
let binding = content.split("\n").collect::<Vec<_>>()[1]
.split(":")
.collect::<Vec<_>>()[1]
.replace(" ", "");
let distances = binding
.split(" ")
.collect::<Vec<_>>()
.into_iter()
.filter(|x| !x.is_empty())
.collect::<Vec<_>>()[0];
let races = c![(times.parse::<i64>().unwrap(), distances.parse::<i64>().unwrap()), for i in 0..times.len()];
let result = c![get_possible_answers(x.0,x.1), for x in races][0];
return result;
}
fn get_possible_answers(time:i64, distance:i64) -> i64{
let list_of_nums_to_check = c![x+1, for x in 0..((((time)/2) as f64).ceil()) as i64];
let result_list = c![i,for i in list_of_nums_to_check.iter().rev().collect::<Vec<_>>(), if i*(time-i) > distance];