From 819da8d94aeefbb1f9df9be14a6b5bfb17a9c0a5 Mon Sep 17 00:00:00 2001 From: leca Date: Sat, 21 Dec 2024 19:34:27 +0300 Subject: [PATCH] added search of 4-color sequences --- main.c | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/main.c b/main.c index a5a8251..45d60ab 100644 --- a/main.c +++ b/main.c @@ -1,14 +1,21 @@ #include #include #include +#include -#define YELLOW "\033[1;33m#" #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; @@ -17,10 +24,13 @@ typedef struct 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) { @@ -40,3 +50,38 @@ void print_garland(Garland* g) { 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; +}