Skip to content

Commit

Permalink
Implement day 06 and 07.
Browse files Browse the repository at this point in the history
  • Loading branch information
Oberacda committed Dec 10, 2023
1 parent ae98bcd commit 8c6e648
Show file tree
Hide file tree
Showing 13 changed files with 1,920 additions and 8 deletions.
15 changes: 13 additions & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ jobs:
toolchain: stable
- uses: Swatinem/rust-cache@v2
- uses: taiki-e/install-action@nextest
- uses: taiki-e/install-action@llvm-cov
- name: Clippy
uses: actions-rs/cargo@v1
with:
Expand All @@ -36,8 +37,18 @@ jobs:
- name: Test
uses: actions-rs/cargo@v1
with:
command: nextest
args: run
command: llvm-cov
args: nextest
- name: Export Coverage Report
uses: actions-rs/cargo@v1
with:
command: llvm-cov
args: report --codecov --output-path ./coverage.xml
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
fail_ci_if_error: true
files: ./coverage.xml,
- name: Publish Test Results
uses: EnricoMi/publish-unit-test-result-action@v2
if: always()
Expand Down
23 changes: 23 additions & 0 deletions day06/Cargo.toml
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"
4 changes: 2 additions & 2 deletions day06/resources/input_part2.txt
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
4 changes: 2 additions & 2 deletions day06/resources/test_input02.txt
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
25 changes: 24 additions & 1 deletion day06/src/bin/day06_part1.rs
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);
}
}
25 changes: 24 additions & 1 deletion day06/src/bin/day06_part2.rs
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);
}
}
37 changes: 37 additions & 0 deletions day06/src/lib.rs
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
*/}
15 changes: 15 additions & 0 deletions day07/Cargo.toml
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]
Loading

0 comments on commit 8c6e648

Please sign in to comment.