Compare commits

..

5 Commits

Author SHA1 Message Date
c28c544304 update CMakeLists.txt 2024-12-21 19:41:16 +03:00
2ed44a7b4b mv main.c src 2024-12-21 19:40:53 +03:00
819da8d94a added search of 4-color sequences 2024-12-21 19:34:27 +03:00
ff80b3a438 added CMakeLists.txt and simplified code 2024-12-21 18:20:33 +03:00
ee749869a5 update gitignore 2024-12-21 18:17:35 +03:00
4 changed files with 102 additions and 60 deletions

10
.gitignore vendored
View File

@@ -52,3 +52,13 @@ Module.symvers
Mkfile.old Mkfile.old
dkms.conf dkms.conf
#CMake
cmake_install.cmake
CMakeFiles
CMakeCache.txt
Makefile
#execs
garland

5
CMakeLists.txt Normal file
View File

@@ -0,0 +1,5 @@
cmake_minimum_required(VERSION 3.10)
project(garland VERSION 1.0 LANGUAGES C)
add_executable(garland src/main.c)

60
main.c
View File

@@ -1,60 +0,0 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define YELLOW "\033[1;33m"
#define RED "\033[1;31m"
#define BLUE "\033[1;34m"
#define GREEN "\033[1;32m"
#define RESET "\033[0m"
typedef struct Garland {
char** lightbulbs;
unsigned int length;
} Garland;
Garland generate_garland(unsigned int length);
void print_garland(Garland* g);
int main () {
Garland g = generate_garland(100);
print_garland(&g);
}
Garland generate_garland(unsigned int length) {
Garland g;
g.lightbulbs = malloc(length * sizeof(char*));
g.length = length;
for (unsigned int i = 0; i < length; i ++) {
char* color = (char*) malloc(9);
switch (rand() % 4) {
case 0:
strncpy(color, RED, 7);
break;
case 1:
strncpy(color, YELLOW, 7);
break;
case 2:
strncpy(color, GREEN, 7);
break;
case 3:
strncpy(color, BLUE, 7);
break;
}
strncpy(color+7, "#\0", 2);
g.lightbulbs[i] = malloc(9);
strncpy(g.lightbulbs[i], color, 9);
free(color);
color = NULL;
}
return g;
}
void print_garland(Garland* g) {
for (unsigned int i = 0; i < g->length; i ++) {
printf("%s", g->lightbulbs[i]);
}
printf(RESET);
printf("\n");
}

87
src/main.c Normal file
View File

@@ -0,0 +1,87 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#define RED "\033[1;31m#"
#define YELLOW "\033[1;33m#"
#define BLUE "\033[1;34m#"
#define GREEN "\033[1;32m#"
#define RESET "\033[0m"
#define RED_FLAG 8
#define YELLOW_FLAG 4
#define BLUE_FLAG 2
#define GREEN_FLAG 1
static char* lightbulbs[] = {RED, YELLOW, BLUE, GREEN};
static int flags[] = {RED_FLAG, YELLOW_FLAG, BLUE_FLAG, GREEN_FLAG};
typedef struct Garland {
char* lightbulbs;
unsigned int length;
} Garland;
Garland generate_garland(unsigned int length);
void print_garland(Garland* g);
int* find_four_different_colors_sequences_in_garland(Garland* g);
int main () {
srand(time(0));
Garland g = generate_garland(100);
print_garland(&g);
find_four_different_colors_sequences_in_garland(&g);
}
Garland generate_garland(unsigned int length) {
Garland g;
g.lightbulbs = malloc(length * sizeof(char*));
g.length = length;
for (unsigned int i = 0; i < length; i ++) {
g.lightbulbs[i] = rand() % 4;
}
return g;
}
void print_garland(Garland* g) {
for (unsigned int i = 0; i < g->length; i ++) {
printf("%s", lightbulbs[g->lightbulbs[i]]);
}
printf(RESET);
printf("\n");
}
int* find_four_different_colors_sequences_in_garland(Garland* g) {
// [0000rybg]
int amount_of_answers = 0;
int *answers = malloc(sizeof(int) * amount_of_answers);
char flag = 0x0;
for (unsigned int i = 0; i < g->length; i ++) {
flag = flag ^ flags[g->lightbulbs[i]];
if (!(flag & flags[g->lightbulbs[i]])) {
flag = 0x0;
i --;
continue;
}
if ((flag ^ 0xf) == 0) {
amount_of_answers += 1;
answers = realloc(answers, amount_of_answers * sizeof(int));
answers[amount_of_answers-1] = i - 3;
flag = 0x0;
}
}
printf("%i\n", amount_of_answers);
for (int i = 0; i < amount_of_answers; i ++) {
printf("place %i: %i (", i, answers[i]);
for (int j = answers[i]; j < answers[i] + 4; j ++) {
printf("%s", lightbulbs[g->lightbulbs[j]]);
}
printf(RESET);
printf(")\n");
}
return answers;
}