Advent of Code 2018
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
1.4 KiB

#include <stdio.h>
#include <glib.h>
int strcmpmatch(const char *a, const char *b)
{
int result = 0;
while (*a && *b) if (*a++ == *b++) result++;
return result;
}
void stripnoncommon(char *a, const char *b)
{
while (*a && *b) {
if (*a++ != *b++) {
memmove(a - 1, a, strlen(a));
a--;
}
}
*a = '\0';
}
int main()
{
gchar *contents;
gchar **strings;
GError *err = NULL;
g_file_get_contents("input", &contents, NULL, &err);
if (err) {
fprintf(stderr, "%s\n", err->message);
g_error_free(err);
return 1;
}
g_strstrip(contents);
strings = g_strsplit(contents, "\n", -1);
g_free(contents);
int bestmatch_len = 0;
char *bestmatch_1 = NULL;
char *bestmatch_2 = NULL;
for (char **first = strings; *first; first++) {
for (char **second = strings; *second; second++) {
if (first == second) continue;
int len = strcmpmatch(*first, *second);
if (len <= bestmatch_len) continue;
bestmatch_1 = *first;
bestmatch_2 = *second;
bestmatch_len = len;
}
}
if (!bestmatch_1 || !bestmatch_2) {
puts("No match found!");
goto end;
}
char *final = g_strdup(bestmatch_1);
stripnoncommon(final, bestmatch_2);
puts(final);
g_free(final);
end:
g_strfreev(strings);
return 0;
}