#include #include #include #define DEFAULT_FILE "input.txt" #define STRBUF_LEN 300 #define ISDIGIT(X) (X >= '0' && X <= '9') #define HASHMAP_SIZE 17576 // 26 * 26 * 26 typedef struct _node { int left; int right; } node; int hashnode(char *str) { bool err = false; if (str[0] < 'A' || str[0] > 'Z') err = true; if (str[1] < 'A' || str[1] > 'Z') err = true; if (str[2] < 'A' || str[2] > 'Z') err = true; if (err) { printf("Error in hashnode!"); return -1; } int first = str[0] - 'A'; int second = str[1] - 'A'; int third = str[2] - 'A'; return (first * 26 * 26) + (second * 26) + third; } int main(int argc, char **argv) { // Get filename as an argument, if there is one char *path = (argc > 1) ? argv[1] : DEFAULT_FILE; if (!path) { printf("Requires input file.\n"); return 1; } FILE *file = fopen(path, "r"); char strbuf[STRBUF_LEN] = {0}; char lrbuf[STRBUF_LEN] = {0}; if (!file) { printf("Could not find file."); return 1; } // Begin Challenge node nodes[HASHMAP_SIZE]; fgets(lrbuf, STRBUF_LEN, file); fgets(strbuf, STRBUF_LEN, file); while(fgets(strbuf, STRBUF_LEN, file)) { node current; int hash = hashnode(strbuf); int left = hashnode(strbuf + 7); int right = hashnode(strbuf + 12); nodes[hash].left = left; nodes[hash].right = right; printf("%s%d %d %d\n\n", strbuf, hash, left, right); } int instructionidx = 0; int currenthash = 0; int numjumps = 0; while(currenthash != HASHMAP_SIZE - 1) { printf("%d\n", currenthash); if (lrbuf[instructionidx] != 'L' && lrbuf[instructionidx] != 'R') instructionidx = 0; bool is_left = lrbuf[instructionidx] == 'L'; instructionidx++; currenthash = is_left ? nodes[currenthash].left : nodes[currenthash].right; numjumps++; } printf("Numjumps: %d\n", numjumps); return 0; }