Skip to content

Commit

Permalink
day 6
Browse files Browse the repository at this point in the history
  • Loading branch information
RensR committed Dec 6, 2023
1 parent f8e616e commit b4fd866
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
2 changes: 2 additions & 0 deletions AdventOfCode/Solutions/sol-2023/input/2023/day6.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time: 59 70 78 78
Distance: 430 1218 1213 1276
2 changes: 1 addition & 1 deletion AdventOfCode/Solutions/sol-2023/src/day5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ fn solve((seed_start, range_length): &(u32, u32), lookup: &Vec<Vec<(u32, u32, u3
mod tests {
use super::{input_generator, part1, part2};

// #[test]
#[test]
fn basics() {
let input = "seeds: 79 14 55 13
Expand Down
53 changes: 53 additions & 0 deletions AdventOfCode/Solutions/sol-2023/src/day6.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#[aoc_generator(day6)]
pub fn input_generator(input: &str) -> Vec<(u64, u64)> {
let sections: Vec<_> = input.split("\n").collect();
return sections[0]
.strip_prefix("Time:")
.unwrap()
.split_whitespace()
.map(|n| n.trim().parse::<u64>().unwrap())
.zip(sections[1]
.strip_prefix("Distance:")
.unwrap()
.split_whitespace()
.map(|n| n.trim().parse::<u64>().unwrap()))
.collect::<Vec<_>>();
}

#[aoc(day6, part1)]
pub fn part1(game: &Vec<(u64, u64)>) -> u64 {
return game
.iter()
.map(|(time, distance)| get_winning_options(*time, *distance))
.fold(1, |acc, n| acc * n);
}

#[aoc(day6, part2)]
pub fn part2(_game: &Vec<(u64, u64)>) -> u64 {
let time :u64 = 59707878;
let distance :u64= 430121812131276;

return get_winning_options(time, distance);
}

fn get_winning_options(time: u64, distance: u64) -> u64 {
let mut total_winners = 0;
for i in 0..= time {
if i * (time - i) > distance {
total_winners += 1;
}
}
return total_winners;
}

#[cfg(test)]
mod tests {
use super::{input_generator, part1};

#[test]
fn basics() {
let input = "Time: 7 15 30
Distance: 9 40 200";
assert_eq!(part1(&input_generator(input)), 288);
}
}
1 change: 1 addition & 0 deletions AdventOfCode/Solutions/sol-2023/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ pub mod day2;
pub mod day3;
pub mod day4;
pub mod day5;
pub mod day6;

aoc_lib! { year = 2023 }

0 comments on commit b4fd866

Please sign in to comment.