github.com/cycloss/advent-of-code@v0.0.0-20221210145555-15039b95faa6/2020/day4/day4-2.c (about) 1 //second version, more robust but more complicated, uses custom hashtable that can be found in my github repos 2 #include <arrayList.h> 3 #include <hashTable.h> 4 #include <stdbool.h> 5 #include <stdio.h> 6 #include <stdlib.h> 7 #include <string.h> 8 9 typedef struct { 10 bool byrOk, iyrOk, eyrOk, hgtOk, hclOk, eclOk, pidOk; 11 } ppValidator; 12 13 void printStringp(void* str) { 14 puts((char*)str); 15 } 16 17 arrayList* getPassports() { 18 FILE* in = fopen("inputFiles/day4.txt", "r"); 19 arrayList* al = createArrayList(); 20 char *buffer = malloc(sizeof(char) * 100), lastChar; 21 int buffInd = 0; 22 23 for (char c = fgetc(in); lastChar != EOF; c = fgetc(in)) { 24 if (c == '\n' && lastChar == '\n' || c == EOF) { 25 buffer[buffInd] = '\0'; 26 appendToAl(al, buffer); 27 buffer = malloc(sizeof(char) * 100); 28 buffInd = 0; 29 } else if (c == ' ' || c == '\n') { 30 buffer[buffInd++] = ' '; 31 } else { 32 buffer[buffInd++] = c; 33 } 34 lastChar = c; 35 } 36 free(buffer); 37 fclose(in); 38 return al; 39 } 40 41 bool checkPpv(ppValidator* ppvp) { 42 for (int i = 0; i < 7; i++) { 43 if (!((bool*)ppvp)[i]) { 44 return false; 45 } 46 } 47 return true; 48 } 49 50 bool partTwo = false; 51 hashTable* eyeColorTable; 52 char* codes[] = { "byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid" }; 53 54 void processSection(char* section, ppValidator* ppvp) { 55 section[3] = '\0'; 56 if (strcmp(section, codes[0]) == 0) { 57 if (partTwo) { 58 section += 4; 59 int year = atoi(section); 60 year >= 1920 && year <= 2002 ? ppvp->byrOk = true : false; 61 } else { 62 ppvp->byrOk = true; 63 } 64 } else if (strcmp(section, codes[1]) == 0) { 65 if (partTwo) { 66 section += 4; 67 int year = atoi(section); 68 year >= 2010 && year <= 2020 ? ppvp->iyrOk = true : false; 69 } else { 70 ppvp->iyrOk = true; 71 } 72 } else if (strcmp(section, codes[2]) == 0) { 73 if (partTwo) { 74 section += 4; 75 int year = atoi(section); 76 year >= 2020 && year <= 2030 ? ppvp->eyrOk = true : false; 77 } else { 78 ppvp->eyrOk = true; 79 } 80 } else if (strcmp(section, codes[3]) == 0) { 81 if (partTwo) { 82 section += 4; 83 int height = atoi(section); 84 int len = strlen(section); 85 if (section[len - 2] == 'c' && section[len - 1] == 'm') { 86 height >= 150 && height <= 193 ? ppvp->hgtOk = true : false; 87 } else if (section[len - 2] == 'i' && section[len - 1] == 'n') { 88 height >= 59 && height <= 76 ? ppvp->hgtOk = true : false; 89 } 90 } else { 91 ppvp->hgtOk = true; 92 } 93 } else if (strcmp(section, codes[4]) == 0) { 94 if (partTwo) { 95 section += 4; 96 int len = strlen(section); 97 bool hash = *section++ == '#'; 98 bool charsOk = true; 99 for (int i = 0; i < len - 1; i++) { 100 char c = section[i]; 101 if (!((section[i] >= '0' && section[i] <= '9') || (section[i] >= 'a' && section[i] <= 'f'))) { 102 charsOk = false; 103 } 104 } 105 hash&& len == 7 && charsOk ? ppvp->hclOk = true : false; 106 } else { 107 ppvp->hclOk = true; 108 } 109 } else if (strcmp(section, codes[5]) == 0) { 110 if (partTwo) { 111 section += 4; 112 ppvp->eclOk = tableContains(eyeColorTable, section); 113 } else { 114 ppvp->eclOk = true; 115 } 116 } else if (strcmp(section, codes[6]) == 0) { 117 if (partTwo) { 118 section += 4; 119 int len = strlen(section); 120 bool numsOk = true; 121 for (int i = 0; i < len; i++) { 122 if (!(section[i] <= '9' && section[i] >= '0')) { 123 numsOk = false; 124 } 125 } 126 len == 9 && numsOk ? ppvp->pidOk = true : false; 127 } else { 128 ppvp->pidOk = true; 129 } 130 } 131 } 132 133 bool checkPassport(char* passport) { 134 135 ppValidator ppv = { false, false, false, false, false, false, false }; 136 for (char* section = strtok(passport, " "); section; section = strtok(NULL, " ")) { 137 processSection(section, &ppv); 138 } 139 return checkPpv(&ppv); 140 } 141 142 int getValidPassports(arrayList* passports) { 143 144 int validCount = 0; 145 for (int i = 0; i < getSize(passports); i++) { 146 if (checkPassport(getItemAt(passports, i))) { 147 validCount++; 148 } 149 } 150 return validCount; 151 } 152 153 int main() { 154 155 arrayList* al = getPassports(); 156 printf("Passport count: %d\n", getSize(al)); 157 printf("Part one valid passports: %d\n", getValidPassports(al)); 158 freeAl(al, true); 159 160 partTwo = true; 161 eyeColorTable = createHashTable(strHash, strComp); 162 addTableItems(eyeColorTable, 7, "amb", "blu", "brn", "gry", "grn", "hzl", "oth"); 163 arrayList* al2 = getPassports(); 164 printf("Part two valid passports: %d\n", getValidPassports(al2)); 165 freeAl(al2, true); 166 freeTable(eyeColorTable, false); 167 }