#include #include #include #include #include "01_hashtable.c" house **process_next_house(house **hash_table, uint32_t *table_size, int32_t *x, int32_t *y, uint32_t *hcount) { //first check to see if we need to enlarge the hash table if (hash(*x, *y) > *table_size - 1) { *table_size = *table_size * 2; hash_table = ht_resize(hash_table, *table_size / 2, *table_size); } //process next house house *loc = ht_lookup(hash_table, *x, *y); if (loc == NULL) { (*hcount)++; ht_insert(hash_table, *x, *y, 1); } else { (loc->p)++; } return hash_table; } int main() { FILE *file = fopen("input", "r"); if (file == NULL) { perror("main: error opening file."); return 1; } uint32_t table_size = 25; house **hash_table = ht_init(table_size); int32_t x = 0; int32_t y = 0; ht_insert(hash_table, x, y, 1); //initial house at (0, 0). uint32_t hcount = 1; int32_t c; while ((c = fgetc(file)) != EOF) { if(c == '^') { y++; hash_table = process_next_house(hash_table, &table_size, &x, &y, &hcount); } else if (c == 'v') { y--; hash_table = process_next_house(hash_table, &table_size, &x, &y, &hcount); } else if (c == '>') { x++; hash_table = process_next_house(hash_table, &table_size, &x, &y, &hcount); } else if (c == '<') { x--; hash_table = process_next_house(hash_table, &table_size, &x, &y, &hcount); } } if (ferror(file)) { perror("main: error reading from file."); return -1; } fclose(file); printf("unique houses: %u\n", hcount); for (uint32_t i = 0; i < table_size; i++) { free(hash_table[i]); } free(hash_table); return 0; }