diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index fbe505c..513e4b2 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -69,8 +69,10 @@ jobs: uses: actions/checkout@v3 - name: Build - run: g++ ./cpp/*.cc - + run: | + for file in cpp/*.cc; do + g++ "$file" -o "${file%.cc}" + done cpp-clang-build: name: Build C++ with clang runs-on: ubuntu-latest @@ -80,7 +82,10 @@ jobs: uses: actions/checkout@v3 - name: Build - run: clang++ ./cpp/*.cc + run: | + for file in cpp/*.cc; do + clang++ "$file" -o "${file%.cc}" + done gcc-build: name: Build C with gcc @@ -91,7 +96,10 @@ jobs: uses: actions/checkout@v3 - name: Build - run: gcc ./c/*.c + run: | + for file in c/*.c; do + gcc "$file" -o "${file%.c}" + done clang-build: name: Build C with clang @@ -102,4 +110,7 @@ jobs: uses: actions/checkout@v3 - name: Build - run: clang ./c/*.c + run: | + for file in c/*.c; do + clang "$file" -o "${file%.c}" + done diff --git a/README.md b/README.md index e7d6557..a98124a 100644 --- a/README.md +++ b/README.md @@ -4,9 +4,12 @@ This is my advent of code 2024 adventure! My goal this year is (I hope so) to resolve everyday challenges using rust. If I have time, I create a solution for C/C++. +Ideally, the solution does not use external crates/dependencies. + ## Solutions | Day | Rust | | C++ | | C | | | :---: | :----: | :----: | :----: | :----: | :----: | :----: | | | Part 1 | Part 2 | Part 1 | Part 2 | Part 1 | Part 2 | -| 1 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | +| 1 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | +| 2 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | diff --git a/c/day02.c b/c/day02.c new file mode 100644 index 0000000..bc47b39 --- /dev/null +++ b/c/day02.c @@ -0,0 +1,115 @@ +#include +#include +#include +#include + +#define MAX_LINE_LENGTH 256 +#define MAX_VALUES 100 + +bool has_small_difference(int *vec, size_t size) { + for (size_t i = 0; i < size - 1; ++i) { + int diff = abs(vec[i + 1] - vec[i]); + if (diff < 1 || diff > 3) { + return false; + } + } + return true; +} + +bool is_safe(int *e, size_t size) { + bool non_increasing = true; + bool non_decreasing = true; + + for (size_t i = 0; i < size - 1; ++i) { + if (e[i] < e[i + 1]) { + non_increasing = false; + } + if (e[i] > e[i + 1]) { + non_decreasing = false; + } + } + + return (non_increasing || non_decreasing) && has_small_difference(e, size); +} + +int **read_file(const char *input, size_t *row_count) { + FILE *file = fopen(input, "r"); + if (!file) { + perror("Failed to open file"); + return NULL; + } + + int **report = malloc(MAX_VALUES * sizeof(int *)); + *row_count = 0; + + char line[MAX_LINE_LENGTH]; + while (fgets(line, sizeof(line), file) && *row_count < MAX_VALUES) { + int *row = malloc(MAX_VALUES * sizeof(int)); + char *token = strtok(line, " "); + size_t col_count = 0; + + while (token != NULL) { + row[col_count++] = atoi(token); + token = strtok(NULL, " "); + } + report[*row_count] = realloc(row, col_count * sizeof(int)); + (*row_count)++; + } + + fclose(file); + return report; +} + +int main() { + size_t row_count; + int **report = read_file("crates/day02/input.txt", &row_count); + if (!report) { + return EXIT_FAILURE; + } + + // --- Part One --- + unsigned int safe_count = 0; + for (size_t i = 0; i < row_count; ++i) { + if (is_safe(report[i], MAX_VALUES)) { + safe_count++; + } + } + printf("Part One solution: sum is %u\n", safe_count); + + // --- Part Two --- + safe_count = 0; + for (size_t i = 0; i < row_count; ++i) { + if (is_safe(report[i], MAX_VALUES)) { + safe_count++; + } else { + for (size_t pos = 0; pos < MAX_VALUES; ++pos) { + if (report[i][pos] == 0) + continue; + int *new = malloc((MAX_VALUES - 1) * sizeof(int)); + size_t new_count = 0; + + for (size_t j = 0; j < MAX_VALUES; ++j) { + if (j != pos) { + new[new_count++] = report[i][j]; + } + } + + if (is_safe(new, new_count)) { + safe_count++; + free(new); + break; + } + + free(new); + } + } + } + printf("Part Two solution: sum is %u\n", safe_count); + + for (size_t i = 0; i < row_count; ++i) { + free(report[i]); + } + free(report); + + return 0; +} diff --git a/cpp/day02.cc b/cpp/day02.cc new file mode 100644 index 0000000..ad9cb4f --- /dev/null +++ b/cpp/day02.cc @@ -0,0 +1,76 @@ +#include +#include +#include +#include +#include +#include + +bool has_small_difference(const std::vector &vec) { + for (size_t i = 0; i < vec.size() - 1; ++i) { + int diff = std::abs(vec[i + 1] - vec[i]); + if (diff < 1 || diff > 3) { + return false; + } + } + return true; +} + +bool is_safe(const std::vector &e) { + bool test = has_small_difference(e); + bool all_non_increasing = + std::is_sorted(e.begin(), e.end(), std::greater()); + bool all_non_decreasing = std::is_sorted(e.begin(), e.end()); + return (all_non_increasing || all_non_decreasing) && test; +} + +std::vector> read_file(const std::string &input) { + std::vector> report; + std::ifstream file(input); + std::string line; + + while (std::getline(file, line)) { + std::istringstream iss(line); + std::vector row; + int num; + while (iss >> num) { + row.push_back(num); + } + report.push_back(row); + } + + return report; +} + +int main() { + // --- Part One --- + auto report = read_file("crates/day02/input.txt"); + unsigned int safe = 0; + + for (const auto &e : report) { + if (is_safe(e)) { + safe++; + } + } + std::cout << "Part One solution: sum is " << safe << std::endl; + + // --- Part Two --- + safe = 0; + for (const auto &e : report) { + if (is_safe(e)) { + safe++; + } else { + for (size_t pos = 0; pos < e.size(); ++pos) { + std::vector new_e = e; + new_e.erase(new_e.begin() + pos); + + if (is_safe(new_e)) { + safe++; + break; + } + } + } + } + std::cout << "Part Two solution: sum is " << safe << std::endl; + + return 0; +} diff --git a/crates/day02/Cargo.toml b/crates/day02/Cargo.toml index 7b9f06c..4df9a8c 100644 --- a/crates/day02/Cargo.toml +++ b/crates/day02/Cargo.toml @@ -4,8 +4,4 @@ version = "0.1.0" edition = "2021" [lints] -workspace = true - -[features] -default = ["unstable"] -unstable = [] \ No newline at end of file +workspace = true \ No newline at end of file diff --git a/crates/day02/src/main.rs b/crates/day02/src/main.rs index b0dc659..8f635a2 100644 --- a/crates/day02/src/main.rs +++ b/crates/day02/src/main.rs @@ -10,19 +10,8 @@ fn has_small_difference(vec: &[i32]) -> bool { }) } fn is_safe(e: &[i32]) -> bool { - let mut sorted = e.to_vec(); - #[cfg(feature = "unstable")] - { - sorted.sort_unstable(); - } - #[cfg(not(feature = "unstable"))] - { - #[allow(clippy::stable_sort_primitive)] - sorted.sort(); - } - let test = has_small_difference(e); - (sorted == e || sorted.iter().rev().eq(e.iter())) && test + (e.windows(2).all(|w| w[0] >= w[1]) || e.windows(2).all(|w| w[0] <= w[1])) && test } fn main() { // --- Part One ---