2022 Day 2 Part 2
This commit is contained in:
parent
ab94510404
commit
3d057cfd04
1 changed files with 31 additions and 3 deletions
34
2022/day2.c
34
2022/day2.c
|
@ -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);
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue