Cut down runtime on part 2 of day 6 by 10 times
This commit is contained in:
parent
fd25e8f8db
commit
4970f247f3
1 changed files with 10 additions and 4 deletions
|
@ -8,6 +8,7 @@ type NumericValue = u64;
|
|||
pub fn solve_task_1(input: &str) -> String {
|
||||
let parsed = parsing::parsing(input);
|
||||
let mut how_many_times_won = 1;
|
||||
|
||||
for next_record in parsed {
|
||||
how_many_times_won *= all_rounds_of_durations(next_record.time())
|
||||
.into_iter()
|
||||
|
@ -48,21 +49,26 @@ pub fn solve_task_2(input: &str) -> String {
|
|||
unreachable!();
|
||||
}
|
||||
|
||||
fn all_rounds_of_durations(duration: NumericValue) -> Vec<NumericValue> {
|
||||
fn all_rounds_of_durations(duration: NumericValue) -> impl Iterator<Item = NumericValue> {
|
||||
let limit = duration as usize;
|
||||
std::iter::repeat(duration)
|
||||
.take(duration as usize)
|
||||
.take(limit)
|
||||
.enumerate()
|
||||
.map(|(speed_duration, duration)| {
|
||||
simuluate_reached_distance_at(duration, speed_duration as NumericValue)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn simuluate_reached_distance_at(
|
||||
duration: NumericValue,
|
||||
speed_duration: NumericValue,
|
||||
) -> NumericValue {
|
||||
assert!(duration >= speed_duration);
|
||||
assert!(
|
||||
duration >= speed_duration,
|
||||
"Duration {:?}. Speed Duration: {:?}",
|
||||
duration,
|
||||
speed_duration
|
||||
);
|
||||
let rest_time = duration - speed_duration;
|
||||
rest_time * speed_duration
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue