finish 02

This commit is contained in:
Xamora 2024-12-02 06:48:31 +01:00
parent e1567cc6c5
commit 8eef1a5fe2
10 changed files with 1212 additions and 0 deletions

25
02/_.h Normal file
View file

@ -0,0 +1,25 @@
#pragma once
#include <ctype.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <strings.h>
#include <wctype.h>
#include <math.h>
#define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
#define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
#define ABS(X) ((X) >= 0 ? (X) : -(X))
char** str_split(char* a_str, const char a_delim);
int str_index(char *str, char c);
int number_of_element(char *str, char c);
long gmc(long a, long b);
long lmc(long a, long b);
long lmcs(int array[], int n);

BIN
02/a.out Executable file

Binary file not shown.

1000
02/main.txt Normal file

File diff suppressed because it is too large Load diff

84
02/mylib.c Normal file
View file

@ -0,0 +1,84 @@
#include "_.h"
// NOT MY CODE
// https://stackoverflow.com/questions/9210528/split-string-with-delimiters-in-c
char** str_split(char* str, const char a_delim)
{
char *a_str = strdup(str);
char** result = 0;
size_t count = 0;
char* tmp = a_str;
char* last_comma = 0;
char delim[2];
delim[0] = a_delim;
delim[1] = 0;
while (*tmp)
{
if (a_delim == *tmp)
{
count++;
last_comma = tmp;
}
tmp++;
}
count += last_comma < (a_str + strlen(a_str) - 1);
count++;
result = malloc(sizeof(char*) * count);
if (result)
{
size_t idx = 0;
char* token = strtok(a_str, delim);
while (token)
{
//assert(idx < count);
*(result + idx++) = strdup(token);
token = strtok(0, delim);
}
//assert(idx == count - 1);
*(result + idx) = 0;
}
free(a_str);
return result;
}
int str_index(char *str, char c) {
int i = 0;
while (str[i])
if (str[i++] == c)
return i;
return -1;
}
int number_of_element(char *str, char c) {
int nbr = 0;
for (int i = 0; str[i] != '\0'; i++)
if (str[i] == c)
nbr++;
return nbr;
}
long gmc(long a, long b) {
if (b == 0)
return a;
return gmc(b, a % b);
}
long lmc(long a, long b) {
return (a / gmc(a, b)) * b;
}
long lmcs(int array[], int n) {
long result = array[0];
for (int i = 1; i < n; i++)
result = lmc(result, array[i]);
return result;
}

37
02/part1.c Normal file
View file

@ -0,0 +1,37 @@
#include "_.h"
int main(int argc, char **argv) {
if (argc != 2)
return 1;
char *input = argv[1];
char **inputs = str_split(strdup(input), '\n');
int count_safe = 0;
for (int i = 0; inputs[i] != NULL; i++) {
char **inputs_space = str_split(strdup(inputs[i]), ' ');
int is_safe = 1;
int increase = 0;
for (int j = 0; inputs_space[j + 1] != NULL; j++) {
int number = atoi(inputs_space[j]);
int next_number = atoi(inputs_space[j + 1]);
if (number - next_number > 0 && number - next_number < 4 && (increase == 1 || increase == 0)) {
increase = 1;
}
else if (number - next_number < 0 && number - next_number > -4 && (increase == -1 || increase == 0)) {
increase = -1;
}
else {
is_safe = 0;
}
}
if (is_safe == 1)
count_safe++;
}
printf("safe: %d\n", count_safe);
return 0;
}

1
02/part1.sh Executable file
View file

@ -0,0 +1 @@
gcc -g -Wall -Wextra -include_.h part1.c mylib.c

57
02/part2.c Normal file
View file

@ -0,0 +1,57 @@
#include "_.h"
int main(int argc, char **argv) {
if (argc != 2)
return 1;
char *input = argv[1];
char **inputs = str_split(strdup(input), '\n');
int count_safe = 0;
for (int i = 0; inputs[i] != NULL; i++) {
char **inputs_space_first = str_split(strdup(inputs[i]), ' ');
int is_safe = 0;
for (int y = -1; inputs_space_first[y] != NULL; y++) {
if (is_safe == 1)
break;
int increase = 0;
is_safe = 1;
char **inputs_space = str_split(strdup(inputs[i]), ' ');
if (y > -1)
inputs_space[y] = "-1";
for (int j = 0; inputs_space[j + 1] != NULL; j++) {
int number = atoi(inputs_space[j]);
int next_number = atoi(inputs_space[j + 1]);
if (number == -1) {
if (inputs_space[j + 2] == NULL)
break;
number = atoi(inputs_space[j + 1]);
next_number = atoi(inputs_space[j + 2]);
}
if (next_number == -1) {
if (inputs_space[j + 2] == NULL)
break;
next_number = atoi(inputs_space[j + 2]);
}
if (number - next_number > 0 && number - next_number < 4 && (increase == 1 || increase == 0)) {
increase = 1;
}
else if (number - next_number < 0 && number - next_number > -4 && (increase == -1 || increase == 0)) {
increase = -1;
}
else {
is_safe = 0;
break;
}
}
}
if (is_safe == 1)
count_safe++;
}
printf("safe: %d\n", count_safe);
return 0;
}

1
02/part2.sh Executable file
View file

@ -0,0 +1 @@
gcc -g -Wall -Wextra -include_.h part2.c mylib.c

6
02/test.txt Normal file
View file

@ -0,0 +1,6 @@
7 6 4 2 1
1 2 7 8 9
9 7 6 2 1
1 3 2 4 5
8 6 4 4 1
1 3 6 7 9

1
02/test2.txt Normal file
View file

@ -0,0 +1 @@
35 37 38 41 43 41