-
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
13 changed files
with
1,920 additions
and
8 deletions.
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
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,23 @@ | ||
[package] | ||
name = "day06" | ||
version = "0.1.0" | ||
authors.workspace = true | ||
edition.workspace = true | ||
description.workspace = true | ||
publish.workspace = true | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[lib] | ||
name = "day06" | ||
path = "src/lib.rs" | ||
|
||
|
||
[[bin]] | ||
name = "day06_part1" | ||
|
||
[[bin]] | ||
name = "day06_part2" | ||
|
||
[dependencies] | ||
rayon = "1.8.0" |
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
Time: 44 70 70 80 | ||
Distance: 283 1134 1134 1491 | ||
Time: 44707080 | ||
Distance: 283113411341491 |
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
Time: 7 15 30 | ||
Distance: 9 40 200 | ||
Time: 71530 | ||
Distance: 940200 |
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 |
---|---|---|
@@ -1,3 +1,26 @@ | ||
use std::time::Instant; | ||
use day06::{parse_input, get_winning_race_strategy_count}; | ||
|
||
fn main() { | ||
println!("Hello, world!"); | ||
let input = include_str!("../../resources/input_part1.txt"); | ||
let now = Instant::now(); | ||
let races = parse_input(input); | ||
let result = get_winning_race_strategy_count(&races); | ||
let elapsed = now.elapsed(); | ||
|
||
println!("Solution: {}; Elapsed: {:?}", result, elapsed); | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_input() { | ||
let input = include_str!("../../resources/test_input01.txt"); | ||
let races = parse_input(input); | ||
println!("Races: {:?}", races); | ||
let result = get_winning_race_strategy_count(&races); | ||
assert_eq!(288, result); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,26 @@ | ||
use std::time::Instant; | ||
use day06::{parse_input, get_winning_race_strategy_count}; | ||
|
||
fn main() { | ||
println!("Hello, world!"); | ||
let input = include_str!("../../resources/input_part2.txt"); | ||
let now = Instant::now(); | ||
let races = parse_input(input); | ||
let result = get_winning_race_strategy_count(&races); | ||
let elapsed = now.elapsed(); | ||
|
||
println!("Solution: {}; Elapsed: {:?}", result, elapsed); | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_input() { | ||
let input = include_str!("../../resources/test_input02.txt"); | ||
let races = parse_input(input); | ||
println!("Races: {:?}", races); | ||
let result = get_winning_race_strategy_count(&races); | ||
assert_eq!(71503, result); | ||
} | ||
} |
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,37 @@ | ||
use std::str::FromStr; | ||
use std::time::Duration; | ||
use rayon::prelude::*; | ||
|
||
#[derive(Default, Debug)] | ||
pub struct Race { | ||
duration: Duration, | ||
distance_mm: u64 | ||
} | ||
|
||
pub fn parse_input(input: &str) -> Vec<Race> { | ||
let lines: Vec<&str> = input.lines().collect(); | ||
let times_iter = lines[0].split_terminator(':').nth(1).unwrap().split_whitespace().map(|x| u64::from_str(x).unwrap()); | ||
let distances_iter = lines[1].split_terminator(':').nth(1).unwrap().split_whitespace().map(|x| u64::from_str(x).unwrap()); | ||
times_iter.zip(distances_iter).map(|(time, distance)| Race {duration: Duration::from_millis(time), distance_mm: distance}).collect() | ||
} | ||
|
||
fn get_race_distance_for_push_duration_mm(duration: &Duration, push_duration: &Duration) -> u64 { | ||
let race_duration = *duration - *push_duration; | ||
let speed_mm_ms = push_duration.as_millis() as u64; | ||
|
||
race_duration.as_millis() as u64 * speed_mm_ms | ||
} | ||
|
||
pub fn get_winning_race_strategy_count(races: &[Race]) -> u64 { | ||
races.par_iter().map(|race| { | ||
let possible_push_durations_ms = race.duration.as_millis() as u64; | ||
(0..possible_push_durations_ms).into_par_iter().map(|x| get_race_distance_for_push_duration_mm(&race.duration, &Duration::from_millis(x))).filter(|x| *x > race.distance_mm).count() as u64 | ||
}).product() | ||
|
||
/* for race in races.iter() { | ||
let possible_push_durations_ms = race.duration.as_millis() as u64; | ||
let winning_strategies: u64 = (0..possible_push_durations_ms).into_par_iter().map(|x| get_race_distance_for_push_duration_mm(&race.duration, &Duration::from_millis(x))).filter(|x| *x > race.distance_mm).count() as u64; | ||
result *= winning_strategies; | ||
} | ||
result | ||
*/} |
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,15 @@ | ||
[package] | ||
name = "day07" | ||
version = "0.1.0" | ||
authors.workspace = true | ||
edition.workspace = true | ||
description.workspace = true | ||
publish.workspace = true | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[lib] | ||
name = "day07" | ||
path = "src/lib.rs" | ||
|
||
[dependencies] |
Oops, something went wrong.