From 760686db4c9173d9ab1b4b4578af7d26066dbd40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20LUDWIG?= Date: Fri, 1 Dec 2023 17:26:01 +0100 Subject: [PATCH] ci: add automated tests/linting/format --- .editorconfig | 15 +++++++++++++++ .gitattributes | 1 + .github/workflows/ci.yml | 24 ++++++++++++++++++++++++ .gitignore | 1 + day_1/Cargo.lock => Cargo.lock | 4 ++++ Cargo.toml | 6 ++++++ README.md | 13 ++++++------- day_1/src/main.rs | 26 +++++++++++++++++--------- day_2/.gitignore | 1 + day_2/Cargo.toml | 6 ++++++ day_2/README.md | 7 +++++++ day_2/src/main.rs | 18 ++++++++++++++++++ 12 files changed, 106 insertions(+), 16 deletions(-) create mode 100644 .editorconfig create mode 100644 .gitattributes create mode 100644 .github/workflows/ci.yml create mode 100644 .gitignore rename day_1/Cargo.lock => Cargo.lock (76%) create mode 100644 Cargo.toml create mode 100644 day_2/.gitignore create mode 100644 day_2/Cargo.toml create mode 100644 day_2/README.md create mode 100644 day_2/src/main.rs diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..8b423bf --- /dev/null +++ b/.editorconfig @@ -0,0 +1,15 @@ +# https://editorconfig.org/ + +root = true + +[*] +indent_style = space +indent_size = 4 +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.txt] +trim_trailing_whitespace = unset +insert_final_newline = unset diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..6313b56 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto eol=lf diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..869219e --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,24 @@ +name: "ci" + +on: + push: + branches: ["main"] + pull_request: + branches: ["main"] + +env: + CARGO_TERM_COLOR: "always" + +jobs: + ci: + runs-on: "ubuntu-latest" + + steps: + - uses: "actions/checkout@v4" + + - run: "rustc --version" + + - run: "cargo build --verbose" + - run: "cargo test --verbose" + - run: "cargo clippy --verbose -- -D warnings" + - run: "cargo fmt -- --check" diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/day_1/Cargo.lock b/Cargo.lock similarity index 76% rename from day_1/Cargo.lock rename to Cargo.lock index 865acc3..64f913b 100644 --- a/day_1/Cargo.lock +++ b/Cargo.lock @@ -5,3 +5,7 @@ version = 3 [[package]] name = "day_1" version = "1.0.0" + +[[package]] +name = "day_2" +version = "1.0.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..038916e --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,6 @@ +[workspace] +members = [ + "day_1", + "day_2", +] +resolver = "2" diff --git a/README.md b/README.md index 30ebb1d..20a350f 100644 --- a/README.md +++ b/README.md @@ -2,16 +2,15 @@ ## About -This repository contains my solutions to the [Advent of Code 2023](https://adventofcode.com/2023) challenges using the [Rust Programming Language](https://www.rust-lang.org/) v1.74.0. - -To run a solution (e.g. Day 1): +This repository contains my solutions for the [Advent of Code 2023](https://adventofcode.com/2023) challenges implemented in the [Rust Programming Language](https://www.rust-lang.org/) v1.74.0. ```sh -cd day_1 -cargo run -``` +# Run the tests (input examples) +cargo test -**Note:** The solutions are not necessarily optimized for performance or readability, but rather for completing the challenge successfully. +# Run a specific day's challenge (e.g. Day 1) +cargo run --package day_1 +``` ## License diff --git a/day_1/src/main.rs b/day_1/src/main.rs index c50a888..06ec0d4 100644 --- a/day_1/src/main.rs +++ b/day_1/src/main.rs @@ -10,7 +10,7 @@ fn part_1(input: &str) -> i32 { let first_digit = characters_digits.first().unwrap_or(&'0').to_owned(); let last_digit = characters_digits.last().unwrap_or(&'0').to_owned(); let number = format!("{}{}", first_digit, last_digit); - let number: i32 = number.parse().expect("Should parse as a number."); + let number: i32 = number.parse().expect("Should parse as a i32."); number }) .sum() @@ -64,15 +64,23 @@ fn part_2(input: &str) -> i32 { } fn main() { - let input_example1 = include_str!("../input_example_1.txt"); - let input_example2 = include_str!("../input_example_2.txt"); let input = include_str!("../input.txt"); - - println!("Answer Part 1 (example1): {}", part_1(input_example1)); - println!("Answer Part 1 (example2): {}", part_1(input_example2)); + println!("- Day 1: Trebuchet?! -"); println!("Answer Part 1: {}", part_1(input)); - - println!("Answer Part 2 (example1): {}", part_2(input_example1)); - println!("Answer Part 2 (example2): {}", part_2(input_example2)); println!("Answer Part 2: {}", part_2(input)); } + +#[cfg(test)] +mod day_1_tests { + use super::*; + + #[test] + fn test_part_1_example() { + assert_eq!(142, part_1(include_str!("../input_example_1.txt"))); + } + + #[test] + fn test_part_2_example() { + assert_eq!(281, part_2(include_str!("../input_example_2.txt"))); + } +} diff --git a/day_2/.gitignore b/day_2/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/day_2/.gitignore @@ -0,0 +1 @@ +/target diff --git a/day_2/Cargo.toml b/day_2/Cargo.toml new file mode 100644 index 0000000..e5fc844 --- /dev/null +++ b/day_2/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day_2" +version = "1.0.0" +edition = "2021" + +[dependencies] diff --git a/day_2/README.md b/day_2/README.md new file mode 100644 index 0000000..79cb764 --- /dev/null +++ b/day_2/README.md @@ -0,0 +1,7 @@ +# - Day 2 - + +Source: + +## Instructions - Part 1 + +## Instructions - Part 2 diff --git a/day_2/src/main.rs b/day_2/src/main.rs new file mode 100644 index 0000000..3eefe75 --- /dev/null +++ b/day_2/src/main.rs @@ -0,0 +1,18 @@ +fn part_1() -> i32 { + 142 +} + +fn main() { + println!("- Day 2: Title -"); + println!("Answer Part 1: {}", part_1()); +} + +#[cfg(test)] +mod day_2_tests { + use super::*; + + #[test] + fn test_part_1_example() { + assert_eq!(142, part_1()); + } +}