#include #include #if 0 #define PART part1 #else #define PART part2 #endif #if 0 #define FILENAME "sample.txt" #else #define FILENAME "input.txt" #endif // outcome[opponent][player] int outcome[3][3] = { /* player rock paper scissors*/ /* rock */ { 1, 2, 0 }, /* paper */ { 0, 1, 2 }, /* scissors */ { 2, 0, 1 }, }; // player[opponent][player] int player[3][3] = { /* lose draw win */ /* rock */ { 2, 0, 1 }, /* paper */ { 0, 1, 2 }, /* scissors */ { 1, 2, 0 }, }; int score = 0; void part1(char *line) { int opponent = line[0] - 'A'; int player = line[2] - 'X'; score += (player + 1) + (outcome[opponent][player] * 3); } void part2(char *line) { int opponent = line[0] - 'A'; int outcome = line[2] - 'X'; score += (player[opponent][outcome] + 1) + (outcome * 3); } void parse() { FILE *fp = fopen(FILENAME, "r"); if(!fp) { fprintf(stderr, "ERROR: Could not open file: %s\n", FILENAME); exit(1); } char line[8]; while(fgets(line, sizeof(line), fp)) PART(line); fclose(fp); } int main(void) { parse(); printf("%d\n", score); return 0; }