Antonio Perrone

Posted on

# Problem Day 6: Wait For It

Here the day 6 problem statement: https://adventofcode.com/2023/day/6

## Solution Part 1

``````pub fn part_one(input: &str) -> Option<u32> {
let lines = input.split('\n').collect::<Vec<_>>();
let times = lines[0].split(':').collect::<Vec<_>>()[1]
.split(' ')
.filter(|c| !c.is_empty())
.collect::<Vec<_>>();
let distances = lines[1].split(':').collect::<Vec<_>>()[1]
.split(' ')
.filter(|c| !c.is_empty())
.collect::<Vec<_>>();

let mut total = 1;

for i in 0..times.len() {
let mut winning = 0;

let time = times[i].parse::<u32>().unwrap();

for j in 0..=time {
let distance = j * (time - j);
if distance > distances[i].parse::<u32>().unwrap() {
winning += 1;
}
}

total *= winning;
}

Some(total)
}
``````

## Solution Part 2

``````pub fn part_two(input: &str) -> Option<u32> {
let lines = input.split('\n').collect::<Vec<_>>();
let time = lines[0].split(':').collect::<Vec<_>>()[1]
.split(' ')
.filter(|c| !c.is_empty())
.collect::<Vec<_>>()
.join("")
.parse::<f64>()
.unwrap();
let distance = lines[1].split(':').collect::<Vec<_>>()[1]
.split(' ')
.filter(|c| !c.is_empty())
.collect::<Vec<_>>()
.join("")
.parse::<f64>()
.unwrap();

let min = (time - (time.powf(2.0) - 4.0 * distance).sqrt()) / 2.0;
let max = (time + (time.powf(2.0) - 4.0 * distance).sqrt()) / 2.0;

Some((max - min + 1.0) as u32)
}
``````

Here the repository with all puzzle solutions.