github.com/cycloss/advent-of-code@v0.0.0-20221210145555-15039b95faa6/2020/day6/day6.c (about) 1 2 #include <arrayList.h> 3 #include <hashTable.h> 4 #include <stdio.h> 5 #include <stdlib.h> 6 #include <string.h> 7 8 #define BUFF_SIZE 40 9 #define ALPH_SIZE 26 10 11 hashTable* ht; 12 int letters[ALPH_SIZE] = { 0 }; 13 14 void printer(void* str) { 15 printf("%s", (char*)str); 16 } 17 18 void addLineCharsToHt(char* line) { 19 for (int i = 0; i < strlen(line); i++) { 20 if (line[i] == '\n') { 21 continue; 22 } 23 char* c = malloc(sizeof(char)); 24 letters[line[i] - 'a']++; 25 *c = line[i]; 26 if (!addTableItem(ht, c)) { 27 free(c); 28 } 29 } 30 } 31 32 int everyoneCount = 0; 33 34 void updateEveryoneCount(int groupSize) { 35 36 for (int i = 0; i < ALPH_SIZE; i++) { 37 if (letters[i] == groupSize) { 38 everyoneCount++; 39 } 40 } 41 memset(letters, 0, sizeof(letters)); 42 } 43 44 int processGroup(arrayList* group) { 45 46 int groupSize = getSize(group); 47 48 for (int i = 0; i < groupSize; i++) { 49 char* line = getItemAt(group, i); 50 addLineCharsToHt(line); 51 } 52 updateEveryoneCount(groupSize); 53 int unique = getTableSize(ht); 54 clearAl(group, true); 55 clearTable(ht, true); 56 return unique; 57 } 58 59 int main() { 60 61 FILE* in = fopen("day6.txt", "r"); 62 63 ht = createHashTable(intHash, intComp); 64 65 arrayList* al = createArrayList(); 66 int total = 0; 67 for (char buff[BUFF_SIZE], *ret = fgets(buff, BUFF_SIZE, in);; ret = fgets(buff, BUFF_SIZE, in)) { 68 if (!ret || buff[0] == '\n') { 69 total += processGroup(al); 70 if (!ret) { 71 break; 72 } 73 } else { 74 char* line = malloc(sizeof(char) * BUFF_SIZE); 75 strcpy(line, buff); 76 appendToAl(al, line); 77 } 78 } 79 printf("\nTotal: %d\n", total); 80 printf("Everyone total: %d\n", everyoneCount); 81 fclose(in); 82 freeAl(al, false); 83 freeTable(ht, false); 84 return 0; 85 }