Skip to content

Commit 2efde27

Browse files
committed
feat: c and c++
1 parent f5d6bcc commit 2efde27

File tree

6 files changed

+213
-23
lines changed

6 files changed

+213
-23
lines changed

.github/workflows/test.yaml

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,10 @@ jobs:
6969
uses: actions/checkout@v3
7070

7171
- name: Build
72-
run: g++ ./cpp/*.cc
73-
72+
run: |
73+
for file in cpp/*.cc; do
74+
g++ "$file" -o "${file%.cc}"
75+
done
7476
cpp-clang-build:
7577
name: Build C++ with clang
7678
runs-on: ubuntu-latest
@@ -80,7 +82,10 @@ jobs:
8082
uses: actions/checkout@v3
8183

8284
- name: Build
83-
run: clang++ ./cpp/*.cc
85+
run: |
86+
for file in cpp/*.cc; do
87+
clang++ "$file" -o "${file%.cc}"
88+
done
8489
8590
gcc-build:
8691
name: Build C with gcc
@@ -91,7 +96,10 @@ jobs:
9196
uses: actions/checkout@v3
9297

9398
- name: Build
94-
run: gcc ./c/*.c
99+
run: |
100+
for file in c/*.c; do
101+
gcc "$file" -o "${file%.c}"
102+
done
95103
96104
clang-build:
97105
name: Build C with clang
@@ -102,4 +110,7 @@ jobs:
102110
uses: actions/checkout@v3
103111

104112
- name: Build
105-
run: clang ./c/*.c
113+
run: |
114+
for file in c/*.c; do
115+
clang "$file" -o "${file%.c}"
116+
done

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ This is my advent of code 2024 adventure!
44

55
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++.
66

7+
Ideally, the solution does not use external crates/dependencies.
8+
79
## Solutions
810

911
| Day | Rust | | C++ | | C | |
1012
| :---: | :----: | :----: | :----: | :----: | :----: | :----: |
1113
| | Part 1 | Part 2 | Part 1 | Part 2 | Part 1 | Part 2 |
12-
| 1 |||||||
14+
| 1 |||||||
15+
| 2 |||||||

c/day02.c

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#include <stdbool.h>
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
#include <string.h>
5+
6+
#define MAX_LINE_LENGTH 256
7+
#define MAX_VALUES 100
8+
9+
bool has_small_difference(int *vec, size_t size) {
10+
for (size_t i = 0; i < size - 1; ++i) {
11+
int diff = abs(vec[i + 1] - vec[i]);
12+
if (diff < 1 || diff > 3) {
13+
return false;
14+
}
15+
}
16+
return true;
17+
}
18+
19+
bool is_safe(int *e, size_t size) {
20+
bool non_increasing = true;
21+
bool non_decreasing = true;
22+
23+
for (size_t i = 0; i < size - 1; ++i) {
24+
if (e[i] < e[i + 1]) {
25+
non_increasing = false;
26+
}
27+
if (e[i] > e[i + 1]) {
28+
non_decreasing = false;
29+
}
30+
}
31+
32+
return (non_increasing || non_decreasing) && has_small_difference(e, size);
33+
}
34+
35+
int **read_file(const char *input, size_t *row_count) {
36+
FILE *file = fopen(input, "r");
37+
if (!file) {
38+
perror("Failed to open file");
39+
return NULL;
40+
}
41+
42+
int **report = malloc(MAX_VALUES * sizeof(int *));
43+
*row_count = 0;
44+
45+
char line[MAX_LINE_LENGTH];
46+
while (fgets(line, sizeof(line), file) && *row_count < MAX_VALUES) {
47+
int *row = malloc(MAX_VALUES * sizeof(int));
48+
char *token = strtok(line, " ");
49+
size_t col_count = 0;
50+
51+
while (token != NULL) {
52+
row[col_count++] = atoi(token);
53+
token = strtok(NULL, " ");
54+
}
55+
report[*row_count] = realloc(row, col_count * sizeof(int));
56+
(*row_count)++;
57+
}
58+
59+
fclose(file);
60+
return report;
61+
}
62+
63+
int main() {
64+
size_t row_count;
65+
int **report = read_file("crates/day02/input.txt", &row_count);
66+
if (!report) {
67+
return EXIT_FAILURE;
68+
}
69+
70+
// --- Part One ---
71+
unsigned int safe_count = 0;
72+
for (size_t i = 0; i < row_count; ++i) {
73+
if (is_safe(report[i], MAX_VALUES)) {
74+
safe_count++;
75+
}
76+
}
77+
printf("Part One solution: sum is %u\n", safe_count);
78+
79+
// --- Part Two ---
80+
safe_count = 0;
81+
for (size_t i = 0; i < row_count; ++i) {
82+
if (is_safe(report[i], MAX_VALUES)) {
83+
safe_count++;
84+
} else {
85+
for (size_t pos = 0; pos < MAX_VALUES; ++pos) {
86+
if (report[i][pos] == 0)
87+
continue;
88+
int *new = malloc((MAX_VALUES - 1) * sizeof(int));
89+
size_t new_count = 0;
90+
91+
for (size_t j = 0; j < MAX_VALUES; ++j) {
92+
if (j != pos) {
93+
new[new_count++] = report[i][j];
94+
}
95+
}
96+
97+
if (is_safe(new, new_count)) {
98+
safe_count++;
99+
free(new);
100+
break;
101+
}
102+
103+
free(new);
104+
}
105+
}
106+
}
107+
printf("Part Two solution: sum is %u\n", safe_count);
108+
109+
for (size_t i = 0; i < row_count; ++i) {
110+
free(report[i]);
111+
}
112+
free(report);
113+
114+
return 0;
115+
}

cpp/day02.cc

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#include <algorithm>
2+
#include <cmath>
3+
#include <fstream>
4+
#include <iostream>
5+
#include <sstream>
6+
#include <vector>
7+
8+
bool has_small_difference(const std::vector<int> &vec) {
9+
for (size_t i = 0; i < vec.size() - 1; ++i) {
10+
int diff = std::abs(vec[i + 1] - vec[i]);
11+
if (diff < 1 || diff > 3) {
12+
return false;
13+
}
14+
}
15+
return true;
16+
}
17+
18+
bool is_safe(const std::vector<int> &e) {
19+
bool test = has_small_difference(e);
20+
bool all_non_increasing =
21+
std::is_sorted(e.begin(), e.end(), std::greater<int>());
22+
bool all_non_decreasing = std::is_sorted(e.begin(), e.end());
23+
return (all_non_increasing || all_non_decreasing) && test;
24+
}
25+
26+
std::vector<std::vector<int>> read_file(const std::string &input) {
27+
std::vector<std::vector<int>> report;
28+
std::ifstream file(input);
29+
std::string line;
30+
31+
while (std::getline(file, line)) {
32+
std::istringstream iss(line);
33+
std::vector<int> row;
34+
int num;
35+
while (iss >> num) {
36+
row.push_back(num);
37+
}
38+
report.push_back(row);
39+
}
40+
41+
return report;
42+
}
43+
44+
int main() {
45+
// --- Part One ---
46+
auto report = read_file("crates/day02/input.txt");
47+
unsigned int safe = 0;
48+
49+
for (const auto &e : report) {
50+
if (is_safe(e)) {
51+
safe++;
52+
}
53+
}
54+
std::cout << "Part One solution: sum is " << safe << std::endl;
55+
56+
// --- Part Two ---
57+
safe = 0;
58+
for (const auto &e : report) {
59+
if (is_safe(e)) {
60+
safe++;
61+
} else {
62+
for (size_t pos = 0; pos < e.size(); ++pos) {
63+
std::vector<int> new_e = e;
64+
new_e.erase(new_e.begin() + pos);
65+
66+
if (is_safe(new_e)) {
67+
safe++;
68+
break;
69+
}
70+
}
71+
}
72+
}
73+
std::cout << "Part Two solution: sum is " << safe << std::endl;
74+
75+
return 0;
76+
}

crates/day02/Cargo.toml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,4 @@ version = "0.1.0"
44
edition = "2021"
55

66
[lints]
7-
workspace = true
8-
9-
[features]
10-
default = ["unstable"]
11-
unstable = []
7+
workspace = true

crates/day02/src/main.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,8 @@ fn has_small_difference(vec: &[i32]) -> bool {
1010
})
1111
}
1212
fn is_safe(e: &[i32]) -> bool {
13-
let mut sorted = e.to_vec();
14-
#[cfg(feature = "unstable")]
15-
{
16-
sorted.sort_unstable();
17-
}
18-
#[cfg(not(feature = "unstable"))]
19-
{
20-
#[allow(clippy::stable_sort_primitive)]
21-
sorted.sort();
22-
}
23-
2413
let test = has_small_difference(e);
25-
(sorted == e || sorted.iter().rev().eq(e.iter())) && test
14+
(e.windows(2).all(|w| w[0] >= w[1]) || e.windows(2).all(|w| w[0] <= w[1])) && test
2615
}
2716
fn main() {
2817
// --- Part One ---

0 commit comments

Comments
 (0)