2022 Day 2 Part 2

This commit is contained in:
Sven Weidauer 2022-12-02 11:47:37 +01:00
parent ab94510404
commit 3d057cfd04

View file

@ -7,16 +7,44 @@ int outcomes[3][3] = {
/* Scissor */ { 0, 6, 3 } /* Scissor */ { 0, 6, 3 }
}; };
int findMove(char opponent, int outcome) {
for (int i = 0; i < 3; i++) {
if (outcomes[i][opponent - 'A'] == outcome) {
return i;
}
}
return -1;
}
int main() { int main() {
FILE *input = fopen("day2.input", "r"); FILE *input = fopen("day2.input", "r");
int total = 0; int totalPart1 = 0;
int totalPart2 = 0;
while (!feof(input)) { while (!feof(input)) {
char opponent, my; char opponent, my;
fscanf(input, "%c %c\n", &opponent, &my); fscanf(input, "%c %c\n", &opponent, &my);
total += my - 'X' + 1 + outcomes[my - 'X'][opponent - 'A']; 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'];
}
fclose(input); fclose(input);
printf("Total score %d\n", total); printf("Part1: Total score %d\n", totalPart1);
printf("Part2: Total score %d\n", totalPart2);
} }