c 2022 2 part 2

This commit is contained in:
Bill Rossi 2024-11-30 06:22:51 -05:00
parent 4ed3ab8768
commit f319dc2cc9

View File

@ -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;