-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
57 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time: 59 70 78 78 | ||
Distance: 430 1218 1213 1276 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,5 +8,6 @@ pub mod day2; | |
pub mod day3; | ||
pub mod day4; | ||
pub mod day5; | ||
pub mod day6; | ||
|
||
aoc_lib! { year = 2023 } |