feat: 2023 day six

This commit is contained in:
Max Hohlfeld 2023-12-21 10:58:35 +01:00
parent 700065b636
commit 0f5138df92
4 changed files with 70 additions and 0 deletions

View File

@ -0,0 +1,2 @@
Time: 46 82 84 79
Distance: 347 1522 1406 1471

66
src/aoc2023/day06/mod.rs Normal file
View File

@ -0,0 +1,66 @@
pub fn task_one(input: &str) -> String {
let races = parse_input_for_task_one(input);
let mut product = 1_u64;
for race in races {
let uprounded_record_speed = (race.record_distance / race.record_time).ceil();
let mut count = 0;
for x in 0..(race.record_time as u32) {
if x > uprounded_record_speed as u32 && (race.record_time - x as f64) * x as f64 > race.record_distance {
count += 1;
}
}
product *= count
}
product.to_string()
}
pub fn task_two(input: &str) -> String {
let race = parse_input_for_task_two(input);
let uprounded_record_speed = (race.record_distance / race.record_time).ceil();
let mut count = 0;
for x in 0..(race.record_time as u32) {
if x > uprounded_record_speed as u32 && (race.record_time - x as f64) * x as f64 > race.record_distance {
count += 1;
}
}
count.to_string()
}
struct Race {
record_time: f64,
record_distance: f64
}
fn parse_input_for_task_one(input: &str) -> Vec<Race> {
let mut races = Vec::new();
let mut iterator = input.lines();
let (_, times) = iterator.next().unwrap().split_once(':').unwrap();
let (_, distances) = iterator.next().unwrap().split_once(':').unwrap();
for pair in times.split_whitespace().zip(distances.split_whitespace()) {
races.push(Race { record_time: pair.0.parse().unwrap(), record_distance: pair.1.parse().unwrap() });
}
races
}
fn parse_input_for_task_two(input: &str) -> Race {
let mut iterator = input.lines();
let (_, times) = iterator.next().unwrap().split_once(':').unwrap();
let (_, distances) = iterator.next().unwrap().split_once(':').unwrap();
let record_time = times.split_whitespace().collect::<Vec<&str>>().join("").parse().unwrap();
let record_distance = distances.split_whitespace().collect::<Vec<&str>>().join("").parse().unwrap();
Race { record_time, record_distance }
}

View File

@ -2,3 +2,4 @@ pub mod day01;
pub mod day02;
pub mod day03;
pub mod day04;
pub mod day06;

View File

@ -49,6 +49,7 @@ fn main() {
puzzles.push(Puzzle { day: 2, year: 2023, task_one: aoc2023::day02::task_one, task_two: aoc2023::day02::task_two });
puzzles.push(Puzzle { day: 3, year: 2023, task_one: aoc2023::day03::task_one, task_two: aoc2023::day03::task_two });
puzzles.push(Puzzle { day: 4, year: 2023, task_one: aoc2023::day04::task_one, task_two: aoc2023::day04::task_two });
puzzles.push(Puzzle { day: 6, year: 2023, task_one: aoc2023::day06::task_one, task_two: aoc2023::day06::task_two });
puzzles.iter().for_each(|puzzle| puzzle.solve_and_print());
}