Skip to content

Instantly share code, notes, and snippets.

@jcsahnwaldt
Last active April 4, 2023 05:27
Show Gist options
  • Save jcsahnwaldt/c059df6eee99794c9551cd9f6c3dcb7a to your computer and use it in GitHub Desktop.
Save jcsahnwaldt/c059df6eee99794c9551cd9f6c3dcb7a to your computer and use it in GitHub Desktop.
// https://stackoverflow.com/questions/53585022/three-colors-triangles/53588144#53588144
// https://www.codewars.com/kata/insane-coloured-triangles/train/c
#define _GNU_SOURCE
#include <stdlib.h>
#include <string.h>
char triangle(const char *s)
{
char* row = strdup(s);
size_t strSz = strlen(row);
while (strSz > 1) {
char *str = row;
while (*str != '\0') {
if (*str == 'G' && *(str + 1) == 'R') *str = 'B';
else if (*str == 'R' && *(str + 1) == 'G') *str = 'B';
else if (*str == 'B' && *(str + 1) == 'G') *str = 'R';
else if (*str == 'G' && *(str + 1) == 'B') *str = 'R';
else if (*str == 'B' && *(str + 1) == 'R') *str = 'G';
else if (*str == 'R' && *(str + 1) == 'B') *str = 'G';
str++;
}
*(str - 1) = '\0';
strSz--;
}
char ch = row[0];
free(row);
return ch;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment