From 3d057cfd046e0edfad4834ddaae4b55b9fb4309f Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Fri, 2 Dec 2022 11:47:37 +0100 Subject: [PATCH] 2022 Day 2 Part 2 --- 2022/day2.c | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/2022/day2.c b/2022/day2.c index f74b6b3..ba17ca6 100644 --- a/2022/day2.c +++ b/2022/day2.c @@ -7,16 +7,44 @@ int outcomes[3][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() { FILE *input = fopen("day2.input", "r"); - int total = 0; + int totalPart1 = 0; + int totalPart2 = 0; while (!feof(input)) { char 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); - printf("Total score %d\n", total); + printf("Part1: Total score %d\n", totalPart1); + printf("Part2: Total score %d\n", totalPart2); } \ No newline at end of file