Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/run refactor #13

Merged
merged 5 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ jobs:

- name: Generate code coverage
run: |
cargo tarpaulin --verbose --all-features --workspace --timeout 120 --out Xml
cargo tarpaulin --verbose --all-features --workspace --timeout 120 --out xml

- name: Upload to codecov.io
uses: codecov/codecov-action@v2
with:
token: ${{secrets.CODECOV_TOKEN}}
fail_ci_if_error: true
# fail_ci_if_error: true
2 changes: 1 addition & 1 deletion 2022/day01/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ path = "src/lib.rs"

[dependencies]
common = { path = "../../common" }
anyhow = "1"
anyhow = { workspace = true }

[dev-dependencies]
criterion = "0.4"
Expand Down
2 changes: 1 addition & 1 deletion 2022/day01/benches/benchmarks.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 Jedrzej Stuczynski
// Copyright 2022-2023 Jedrzej Stuczynski
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
92 changes: 83 additions & 9 deletions 2022/day01/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 Jedrzej Stuczynski
// Copyright 2022-2023 Jedrzej Stuczynski
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -12,16 +12,90 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use common::execution::execute;
use common::parsing::parse_groups;
use std::path::Path;
#![warn(clippy::unwrap_used)]
#![warn(clippy::expect_used)]

pub use crate::solution::{part1, part2};
pub use types::Elf;
use crate::types::Elf;
use common::parsing::parse_groups;
use common::AocSolution;

mod solution;
mod types;

pub fn solve<P: AsRef<Path>>(input_file: P) {
execute(input_file, parse_groups, part1, part2)
pub struct Day01;

impl AocSolution for Day01 {
type Input = Vec<Elf>;
type Part1Output = usize;
type Part2Output = usize;

fn parse_input<M: AsRef<str>>(raw: M) -> Result<Self::Input, anyhow::Error> {
parse_groups(raw.as_ref())
}

fn part1(input: Self::Input) -> Result<Self::Part1Output, anyhow::Error> {
Ok(part1(input))
}

fn part2(input: Self::Input) -> Result<Self::Part2Output, anyhow::Error> {
Ok(part2(input))
}
}

pub fn part1(input: Vec<Elf>) -> usize {
input
.iter()
.map(|elf| elf.total_calorie_count())
.max()
.unwrap_or_default()
}

pub fn part2(input: Vec<Elf>) -> usize {
// sorting the input and getting 3 last values is O(nlogn),
// meanwhile keeping track of 3 maximum values requires single O(n) iteration
let mut max = usize::MIN;
let mut max2 = usize::MIN;
let mut max3 = usize::MIN;

for total_count in input.iter().map(|elf| elf.total_calorie_count()) {
if total_count > max {
max3 = max2;
max2 = max;
max = total_count
} else if total_count > max2 {
max3 = max2;
max2 = total_count;
} else if total_count > max3 {
max3 = total_count
}
}

max + max2 + max3
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
use super::*;

fn sample_input() -> Vec<Elf> {
vec![
Elf::new(vec![1000, 2000, 3000]),
Elf::new(vec![4000]),
Elf::new(vec![5000, 6000]),
Elf::new(vec![7000, 8000, 9000]),
Elf::new(vec![10000]),
]
}

#[test]
fn part1_sample_input() {
let expected = 24000;
assert_eq!(expected, part1(sample_input()))
}

#[test]
fn part2_sample_input() {
let expected = 45000;
assert_eq!(expected, part2(sample_input()))
}
}
12 changes: 4 additions & 8 deletions 2022/day01/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 Jedrzej Stuczynski
// Copyright 2022-2023 Jedrzej Stuczynski
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -12,14 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::solution::{part1, part2};
use common::execution::execute;
use common::parsing::parse_groups;

mod solution;
mod types;
use common::AocSolutionSolver;
use day01_2022::Day01;

#[cfg(not(tarpaulin))]
fn main() {
execute("input", parse_groups, part1, part2)
Day01::try_solve_from_file("input")
}
73 changes: 0 additions & 73 deletions 2022/day01/src/solution.rs

This file was deleted.

2 changes: 1 addition & 1 deletion 2022/day01/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 Jedrzej Stuczynski
// Copyright 2022-2023 Jedrzej Stuczynski
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion 2022/day02/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ path = "src/lib.rs"

[dependencies]
common = { path = "../../common" }
anyhow = "1"
anyhow = { workspace = true }

[dev-dependencies]
criterion = "0.4"
Expand Down
2 changes: 1 addition & 1 deletion 2022/day02/benches/benchmarks.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 Jedrzej Stuczynski
// Copyright 2022-2023 Jedrzej Stuczynski
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
65 changes: 60 additions & 5 deletions 2022/day02/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 Jedrzej Stuczynski
// Copyright 2022-2023 Jedrzej Stuczynski
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -12,16 +12,71 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#![warn(clippy::unwrap_used)]
#![warn(clippy::expect_used)]

use crate::types::RPSGame;
use common::execution::execute;
use common::parsing::parse_input_lines;
use common::AocSolution;
use std::path::Path;

pub use crate::solution::{part1, part2};
pub use types::RPSGame;

mod solution;
mod types;

pub struct Day02;

impl AocSolution for Day02 {
type Input = Vec<RPSGame>;
type Part1Output = usize;
type Part2Output = usize;

fn parse_input<M: AsRef<str>>(raw: M) -> Result<Self::Input, anyhow::Error> {
parse_input_lines(raw.as_ref())
}

fn part1(input: Self::Input) -> Result<Self::Part1Output, anyhow::Error> {
Ok(part1(input))
}

fn part2(input: Self::Input) -> Result<Self::Part2Output, anyhow::Error> {
Ok(part2(input))
}
}

pub fn part1(input: Vec<RPSGame>) -> usize {
input.into_iter().map(|p| p.play_as_shape()).sum()
}

pub fn part2(input: Vec<RPSGame>) -> usize {
input.into_iter().map(|p| p.play_as_result()).sum()
}

pub fn solve<P: AsRef<Path>>(input_file: P) {
execute(input_file, parse_input_lines, part1, part2)
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
use super::*;

fn sample_input() -> Vec<RPSGame> {
vec![
"A Y".parse().unwrap(),
"B X".parse().unwrap(),
"C Z".parse().unwrap(),
]
}

#[test]
fn part1_sample_input() {
let expected = 15;
assert_eq!(expected, part1(sample_input()))
}

#[test]
fn part2_sample_input() {
let expected = 12;
assert_eq!(expected, part2(sample_input()))
}
}
12 changes: 4 additions & 8 deletions 2022/day02/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 Jedrzej Stuczynski
// Copyright 2022-2023 Jedrzej Stuczynski
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -12,14 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::solution::{part1, part2};
use common::execution::execute;
use common::parsing::parse_input_lines;

mod solution;
mod types;
use common::AocSolutionSolver;
use day02_2022::Day02;

#[cfg(not(tarpaulin))]
fn main() {
execute("input", parse_input_lines, part1, part2)
Day02::try_solve_from_file("input")
}
Loading
Loading