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.

58 lines
1.2 KiB

#include <stdlib.h>
#include <stdio.h>
int compare_char(const void *a, const void *b)
{
return *(const char *)a - *(const char *)b;
}
char find_reoccurrence(const char *array, const ssize_t len, const int times)
{
if (!len) return 0;
if (!times) return 0;
char current = *array;
int count = 0;
for (const char *i = array; i < array + len; i++) {
if (*i == current) {
count++;
continue;
}
if (count == times) return current;
current = *i;
count = 1;
}
return count == times ? current : 0;
}
int main()
{
char *line = NULL;
size_t line_len = 0;
ssize_t len = 0;
FILE *f = fopen("input", "r");
if (!f) {
perror("input");
return 1;
}
int twice = 0;
int thrice = 0;
while ((len = getline(&line, &line_len, f)) != -1) {
while (line[len - 1] == '\n') line[--len] = '\0';
qsort(line, len, sizeof(char), compare_char);
if (find_reoccurrence(line, len, 2)) twice++;
if (find_reoccurrence(line, len, 3)) thrice++;
}
printf("%d\n", twice * thrice);
free(line);
fclose(f);
return 0;
}