aoc_omni/c/2022/2/rock_paper_scissors.c

38 lines
906 B
C
Raw Normal View History

2024-11-29 21:27:53 -05:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../../lib/aoc.h"
2024-11-30 06:22:51 -05:00
int round_score_p1(const char *round) {
2024-11-29 21:27:53 -05:00
int opponent_shape = round[0] - 'A' + 1;
2024-11-30 06:22:51 -05:00
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;
2024-11-29 21:27:53 -05:00
}
int main() {
2024-11-30 06:22:51 -05:00
int total_score_p1 = 0;
int total_score_p2 = 0;
2024-11-29 21:27:53 -05:00
2024-11-30 06:07:36 -05:00
char *round;
while ((round = aoc_read_line()) != NULL) {
2024-11-30 06:22:51 -05:00
total_score_p1 += round_score_p1(round);
total_score_p2 += round_score_p2(round);
2024-11-29 21:27:53 -05:00
}
2024-11-30 06:22:51 -05:00
printf("Part 1: %d\n", total_score_p1);
printf("Part 2: %d\n", total_score_p2);
2024-11-29 21:27:53 -05:00
2024-11-30 06:07:36 -05:00
aoc_free();
2024-11-29 21:27:53 -05:00
return 0;
}