137 lines
3.1 KiB
C
137 lines
3.1 KiB
C
/**
|
|
* Copyright [2023] Jiří Štefka <jiriks74>
|
|
* Project: AdventOfCode
|
|
* @file main.c
|
|
* @brief Main entry point
|
|
* @author jiriks74
|
|
*/
|
|
|
|
#include <stdbool.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
const int MAX_RED = 12;
|
|
const int MAX_GREEN = 13;
|
|
const int MAX_BLUE = 14;
|
|
|
|
typedef struct {
|
|
int red;
|
|
int green;
|
|
int blue;
|
|
} GameShow;
|
|
|
|
/**
|
|
* @brief Count how many cubes of each color were in a show
|
|
* @param *show A show of cubes in a game
|
|
* @return A count of all the cubes in a struct
|
|
*/
|
|
GameShow get_max_cube_counts(char *game) {
|
|
char *curr = game;
|
|
GameShow result = {0};
|
|
|
|
while (curr != NULL) {
|
|
char *end_show = strchr(curr, ';');
|
|
|
|
if (end_show != NULL) {
|
|
*end_show = '\0';
|
|
}
|
|
|
|
while (curr != NULL) {
|
|
int cube_count = atoi(curr);
|
|
|
|
curr = strchr(curr, ' ');
|
|
curr += sizeof(char);
|
|
|
|
if (*curr == 'r') {
|
|
if (cube_count > result.red)
|
|
result.red = cube_count;
|
|
} else if (*curr == 'g') {
|
|
if (cube_count > result.green)
|
|
result.green = cube_count;
|
|
} else if (*curr == 'b') {
|
|
if (cube_count > result.blue)
|
|
result.blue = cube_count;
|
|
} else {
|
|
result.red = -1;
|
|
result.green = -1;
|
|
result.blue = -1;
|
|
return result;
|
|
}
|
|
curr = strchr(curr, ',');
|
|
if (curr != NULL)
|
|
curr += sizeof(char) * 2;
|
|
}
|
|
|
|
if (end_show != NULL) {
|
|
*end_show = ';';
|
|
curr = end_show + sizeof(char) * 2;
|
|
} else
|
|
curr = end_show;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* @brief Count how many times were the cubes shown
|
|
* @return Number of shows or -1 when error occurs
|
|
*/
|
|
bool is_game_possible(char *game) {
|
|
// char *curr = strchr(game, ':');
|
|
// curr += sizeof(char) * 2;
|
|
|
|
// if (curr == NULL) {
|
|
// return false;
|
|
// }
|
|
|
|
GameShow show_counts = get_max_cube_counts(game);
|
|
|
|
if (show_counts.red > MAX_RED)
|
|
return false;
|
|
else if (show_counts.green > MAX_GREEN)
|
|
return false;
|
|
else if (show_counts.blue > MAX_BLUE)
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
#ifndef TESTING
|
|
/**
|
|
* @brief Main entry point
|
|
* @param argc Number of command-line arguments.
|
|
* @param argv Array of command-line arguments.
|
|
*/
|
|
int main(int argc, char *argv[])
|
|
#endif
|
|
#ifdef TESTING
|
|
int main_test(int argc, char *argv[])
|
|
#endif
|
|
{
|
|
FILE *input = fopen(argv[1], "r");
|
|
|
|
int possible_game_ids_sum = 0;
|
|
int sum_of_powers = 0;
|
|
|
|
char line[512];
|
|
while (fgets(line, sizeof(line), input)) {
|
|
if (*line != 'G')
|
|
break;
|
|
|
|
int curr_game_id = atoi(line + sizeof("Game"));
|
|
|
|
// if (is_game_possible(line)) {
|
|
if (is_game_possible(strchr(line, ':') + sizeof(char) * 2)) {
|
|
possible_game_ids_sum += curr_game_id;
|
|
// printf("Game %d is possible\n", curr_game_id);
|
|
} // else
|
|
// printf("Game %d is impossible\n", curr_game_id);
|
|
GameShow min_cubes =
|
|
get_max_cube_counts(strchr(line, ':') + sizeof(char) * 2);
|
|
sum_of_powers += min_cubes.red * min_cubes.green * min_cubes.blue;
|
|
}
|
|
|
|
printf("Sum of possible game IDs: %d\n", possible_game_ids_sum);
|
|
printf("Sum of powers: %d\n", sum_of_powers);
|
|
return 0;
|
|
}
|