-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: rust * feat: c and c++
- Loading branch information
Showing
10 changed files
with
1,288 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,5 @@ edition = "2021" | |
workspace = true | ||
|
||
[features] | ||
default = ["unstable"] | ||
unstable = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.