Skip to content

Commit

Permalink
Merge pull request #14 from jstuczyn/feature/derive-macro
Browse files Browse the repository at this point in the history
Feature/derive macro
  • Loading branch information
jstuczyn authored Nov 11, 2023
2 parents 239313a + eb1ba2c commit 805fe13
Show file tree
Hide file tree
Showing 38 changed files with 782 additions and 389 deletions.
3 changes: 2 additions & 1 deletion 2022/day01/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ name = "day01_2022"
path = "src/lib.rs"

[dependencies]
aoc-solution = { path = "../../aoc-solution" }
common = { path = "../../common" }
anyhow = { workspace = true }

[dev-dependencies]
criterion = "0.4"
criterion = "0.5"

[[bench]]
name = "benchmarks"
Expand Down
27 changes: 7 additions & 20 deletions 2022/day01/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,18 @@
#![warn(clippy::expect_used)]

use crate::types::Elf;
use common::parsing::parse_groups;
use common::AocSolution;
use aoc_solution::Aoc;
use common::parsing::GroupsParser;

mod types;

#[derive(Aoc)]
#[aoc(input = Vec<Elf>)]
#[aoc(parser = GroupsParser)]
#[aoc(part1(output = usize, runner = part1))]
#[aoc(part2(output = usize, runner = 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()
Expand Down
2 changes: 1 addition & 1 deletion 2022/day01/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use common::AocSolutionSolver;
use aoc_solution::AocSolutionSolver;
use day01_2022::Day01;

#[cfg(not(tarpaulin))]
Expand Down
3 changes: 2 additions & 1 deletion 2022/day02/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ name = "day02_2022"
path = "src/lib.rs"

[dependencies]
aoc-solution = { path = "../../aoc-solution" }
common = { path = "../../common" }
anyhow = { workspace = true }

[dev-dependencies]
criterion = "0.4"
criterion = "0.5"

[[bench]]
name = "benchmarks"
Expand Down
33 changes: 7 additions & 26 deletions 2022/day02/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,18 @@
#![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;
use aoc_solution::Aoc;
use common::parsing::LineParser;

mod types;

#[derive(Aoc)]
#[aoc(input = Vec<RPSGame>)]
#[aoc(parser = LineParser)]
#[aoc(part1(output = usize, runner = part1))]
#[aoc(part2(output = usize, runner = part2))]
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()
}
Expand All @@ -51,10 +36,6 @@ 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 {
Expand Down
2 changes: 1 addition & 1 deletion 2022/day02/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use common::AocSolutionSolver;
use aoc_solution::AocSolutionSolver;
use day02_2022::Day02;

#[cfg(not(tarpaulin))]
Expand Down
3 changes: 2 additions & 1 deletion 2022/day03/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ name = "day03_2022"
path = "src/lib.rs"

[dependencies]
aoc-solution = { path = "../../aoc-solution" }
common = { path = "../../common" }
anyhow = { workspace = true }

[dev-dependencies]
criterion = "0.4"
criterion = "0.5"

[[bench]]
name = "benchmarks"
Expand Down
27 changes: 7 additions & 20 deletions 2022/day03/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,18 @@
#![warn(clippy::expect_used)]

use crate::types::Rucksack;
use common::parsing::parse_input_lines;
use common::AocSolution;
use aoc_solution::Aoc;
use common::parsing::LineParser;

mod types;

#[derive(Aoc)]
#[aoc(input = Vec<Rucksack>)]
#[aoc(parser = LineParser)]
#[aoc(part1(output = usize, runner = part1))]
#[aoc(part2(output = usize, runner = part2))]
pub struct Day03;

impl AocSolution for Day03 {
type Input = Vec<Rucksack>;
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<Rucksack>) -> usize {
input
.into_iter()
Expand Down
2 changes: 1 addition & 1 deletion 2022/day03/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use common::AocSolutionSolver;
use aoc_solution::AocSolutionSolver;
use day03_2022::Day03;

#[cfg(not(tarpaulin))]
Expand Down
3 changes: 2 additions & 1 deletion 2022/day04/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ name = "day04_2022"
path = "src/lib.rs"

[dependencies]
aoc-solution = { path = "../../aoc-solution" }
common = { path = "../../common" }
anyhow = { workspace = true }

[dev-dependencies]
criterion = "0.4"
criterion = "0.5"

[[bench]]
name = "benchmarks"
Expand Down
26 changes: 7 additions & 19 deletions 2022/day04/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,18 @@
#![warn(clippy::expect_used)]

use crate::types::AssignmentPair;
use common::parsing::parse_input_lines;
use common::AocSolution;
use aoc_solution::Aoc;
use common::parsing::LineParser;

mod types;

#[derive(Aoc)]
#[aoc(input = Vec<AssignmentPair>)]
#[aoc(parser = LineParser)]
#[aoc(part1(output = usize, runner = part1))]
#[aoc(part2(output = usize, runner = part2))]
pub struct Day04;

impl AocSolution for Day04 {
type Input = Vec<AssignmentPair>;
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<AssignmentPair>) -> usize {
input
.into_iter()
Expand Down
2 changes: 1 addition & 1 deletion 2022/day04/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use common::AocSolutionSolver;
use aoc_solution::AocSolutionSolver;
use day04_2022::Day04;

#[cfg(not(tarpaulin))]
Expand Down
3 changes: 2 additions & 1 deletion 2022/day05/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ name = "day05_2022"
path = "src/lib.rs"

[dependencies]
aoc-solution = { path = "../../aoc-solution" }
common = { path = "../../common" }
anyhow = { workspace = true }

[dev-dependencies]
criterion = "0.4"
criterion = "0.5"

[[bench]]
name = "benchmarks"
Expand Down
26 changes: 7 additions & 19 deletions 2022/day05/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,18 @@
#![warn(clippy::expect_used)]

use crate::types::Supplies;
use common::AocSolution;
use aoc_solution::Aoc;
use common::parsing::FromStrParser;

mod types;

#[derive(Aoc)]
#[aoc(input = Supplies)]
#[aoc(parser = FromStrParser)]
#[aoc(part1(output = String, runner = part1))]
#[aoc(part2(output = String, runner = part2))]
pub struct Day05;

impl AocSolution for Day05 {
type Input = Supplies;
type Part1Output = String;
type Part2Output = String;

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

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(mut input: Supplies) -> String {
input.complete_rearrangement_procedure(false);
input.top_message()
Expand Down
2 changes: 1 addition & 1 deletion 2022/day05/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use common::AocSolutionSolver;
use aoc_solution::AocSolutionSolver;
use day05_2022::Day05;

#[cfg(not(tarpaulin))]
Expand Down
3 changes: 2 additions & 1 deletion 2022/day06/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ name = "day06_2022"
path = "src/lib.rs"

[dependencies]
aoc-solution = { path = "../../aoc-solution" }
common = { path = "../../common" }
anyhow = { workspace = true }

[dev-dependencies]
criterion = "0.4"
criterion = "0.5"

[[bench]]
name = "benchmarks"
Expand Down
27 changes: 7 additions & 20 deletions 2022/day06/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,16 @@
#![warn(clippy::unwrap_used)]
#![warn(clippy::expect_used)]

use common::parsing::as_char_vec;
use common::AocSolution;
use aoc_solution::Aoc;
use common::parsing::CharVecParser;

#[derive(Aoc)]
#[aoc(input = Vec<char>)]
#[aoc(parser = CharVecParser)]
#[aoc(part1(output = usize, runner = part1))]
#[aoc(part2(output = usize, runner = part2))]
pub struct Day06;

impl AocSolution for Day06 {
type Input = Vec<char>;
type Part1Output = usize;
type Part2Output = usize;

fn parse_input<M: AsRef<str>>(raw: M) -> Result<Self::Input, anyhow::Error> {
as_char_vec(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))
}
}

// since our window size is 4 and 14 for part1 and part2 respectively,
// it's more efficient to do full slice lookup as opposed to paying for the instantiation cost of a HashSet
fn solve(input: Vec<char>, window_size: usize) -> usize {
Expand Down
2 changes: 1 addition & 1 deletion 2022/day06/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use common::AocSolutionSolver;
use aoc_solution::AocSolutionSolver;
use day06_2022::Day06;

#[cfg(not(tarpaulin))]
Expand Down
3 changes: 2 additions & 1 deletion 2022/day07/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ name = "day07_2022"
path = "src/lib.rs"

[dependencies]
aoc-solution = { path = "../../aoc-solution" }
common = { path = "../../common" }
anyhow = { workspace = true }

[dev-dependencies]
criterion = "0.4"
criterion = "0.5"

[[bench]]
name = "benchmarks"
Expand Down
Loading

0 comments on commit 805fe13

Please sign in to comment.