From f319dc2cc95f321260ccfdf42b6f784e4e801851 Mon Sep 17 00:00:00 2001 From: Bill Rossi Date: Sat, 30 Nov 2024 06:22:51 -0500 Subject: [PATCH] c 2022 2 part 2 --- c/2022/2/problem.c | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/c/2022/2/problem.c b/c/2022/2/problem.c index e473453..e78a274 100644 --- a/c/2022/2/problem.c +++ b/c/2022/2/problem.c @@ -4,22 +4,33 @@ #include "../../lib/aoc.h" -int round_score(const char *round) { - int shape = round[2] - 'X' + 1; +int round_score_p1(const char *round) { int opponent_shape = round[0] - 'A' + 1; - int result = (((shape - opponent_shape) + 4) % 3) * 3; - return shape + result; + int player_shape = round[2] - 'X' + 1; + int result = (((player_shape - opponent_shape) + 4) % 3) * 3; + return player_shape + result; +} + +int round_score_p2(const char *round) { + int opponent_shape = round[0] - 'A' + 1; + int result = (round[2] - 'X') * 3; + int player_shape = ((result / 3) + 2 + opponent_shape) % 3; + if (player_shape == 0) player_shape = 3; + return player_shape + result; } int main() { - int total_score = 0; + int total_score_p1 = 0; + int total_score_p2 = 0; char *round; while ((round = aoc_read_line()) != NULL) { - total_score += round_score(round); + total_score_p1 += round_score_p1(round); + total_score_p2 += round_score_p2(round); } - printf("Part 1: %d\n", total_score); + printf("Part 1: %d\n", total_score_p1); + printf("Part 2: %d\n", total_score_p2); aoc_free(); return 0;