AoC/2022/day2.c

50 lines
1.2 KiB
C
Raw Normal View History

2022-12-02 11:40:04 +01:00
#include <stdio.h>
int outcomes[3][3] = {
// Rock Paper Scissor
/* Rock */ { 3, 0, 6 },
/* Paper */ { 6, 3, 0 },
/* Scissor */ { 0, 6, 3 }
};
2022-12-02 11:47:37 +01:00
int findMove(char opponent, int outcome) {
for (int i = 0; i < 3; i++) {
if (outcomes[i][opponent - 'A'] == outcome) {
return i;
}
}
return -1;
}
2022-12-02 11:40:04 +01:00
int main() {
FILE *input = fopen("day2.input", "r");
2022-12-02 11:47:37 +01:00
int totalPart1 = 0;
int totalPart2 = 0;
2022-12-02 11:40:04 +01:00
while (!feof(input)) {
char opponent, my;
fscanf(input, "%c %c\n", &opponent, &my);
2022-12-02 11:47:37 +01:00
totalPart1 += my - 'X' + 1 + outcomes[my - 'X'][opponent - 'A'];
switch (my) {
case 'X': // Lose
my = findMove(opponent, 0);
break;
case 'Y': // Draw
my = opponent - 'A';
break;
case 'Z': // Win
my = findMove(opponent, 6);
break;
}
totalPart2 += my + 1 + outcomes[my][opponent - 'A'];
2022-12-02 11:40:04 +01:00
}
2022-12-02 11:47:37 +01:00
2022-12-02 11:40:04 +01:00
fclose(input);
2022-12-02 11:47:37 +01:00
printf("Part1: Total score %d\n", totalPart1);
printf("Part2: Total score %d\n", totalPart2);
2022-12-02 11:40:04 +01:00
}