Skip to content

Commit

Permalink
feat: day02 (#4)
Browse files Browse the repository at this point in the history
* feat: rust

* feat: c and c++
  • Loading branch information
martabal authored Dec 2, 2024
1 parent 9c545ed commit 24dd1d8
Show file tree
Hide file tree
Showing 10 changed files with 1,288 additions and 8 deletions.
21 changes: 16 additions & 5 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
6 changes: 5 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[workspace]
resolver = "2"
members = ["crates/day01"]
members = ["crates/day01", "crates/day02"]

[profile.release]
opt-level = 'z'
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |||||||
115 changes: 115 additions & 0 deletions c/day02.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#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;
}
76 changes: 76 additions & 0 deletions cpp/day02.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include <algorithm>
#include <cmath>
#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>

bool has_small_difference(const std::vector<int> &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<int> &e) {
bool test = has_small_difference(e);
bool all_non_increasing =
std::is_sorted(e.begin(), e.end(), std::greater<int>());
bool all_non_decreasing = std::is_sorted(e.begin(), e.end());
return (all_non_increasing || all_non_decreasing) && test;
}

std::vector<std::vector<int>> read_file(const std::string &input) {
std::vector<std::vector<int>> report;
std::ifstream file(input);
std::string line;

while (std::getline(file, line)) {
std::istringstream iss(line);
std::vector<int> 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<int> 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;
}
1 change: 1 addition & 0 deletions crates/day01/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ edition = "2021"
workspace = true

[features]
default = ["unstable"]
unstable = []
7 changes: 7 additions & 0 deletions crates/day02/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "day02"
version = "0.1.0"
edition = "2021"

[lints]
workspace = true
Loading

0 comments on commit 24dd1d8

Please sign in to comment.