summaryrefslogtreecommitdiff
path: root/Advent-of-Code-2022/aoc-2/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'Advent-of-Code-2022/aoc-2/main.c')
-rw-r--r--Advent-of-Code-2022/aoc-2/main.c70
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;
+}