diff options
author | kartofen <mladenovnasko0@gmail.com> | 2022-12-02 18:53:27 +0200 |
---|---|---|
committer | kartofen <mladenovnasko0@gmail.com> | 2022-12-02 18:53:27 +0200 |
commit | 41085185b9a69e455a72d6beed8f29c52967d21d (patch) | |
tree | eff276573bef8a50b312045270b36e328d95a7a5 /Advent-of-Code-2022/aoc-2/main.c | |
parent | 4e9132b983c771d3e70d5a6b8bcdfdb1fead81fa (diff) |
add 2022
Diffstat (limited to 'Advent-of-Code-2022/aoc-2/main.c')
-rw-r--r-- | Advent-of-Code-2022/aoc-2/main.c | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/Advent-of-Code-2022/aoc-2/main.c b/Advent-of-Code-2022/aoc-2/main.c new file mode 100644 index 0000000..af65b88 --- /dev/null +++ b/Advent-of-Code-2022/aoc-2/main.c @@ -0,0 +1,70 @@ +#include <stdio.h> +#include <stdlib.h> + +#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; +} |