Skip to content

Commit

Permalink
02-2
Browse files Browse the repository at this point in the history
  • Loading branch information
oioki committed Dec 2, 2023
1 parent bc5c92e commit 78d7a92
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions 02/02.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <string.h>


int game_number_if_possible(const char * s, int red_asked, int green_asked, int blue_asked) {
int game_output(const char * s, int red_asked, int green_asked, int blue_asked, int mode) {
int game_number = 0;
int bytes_read;
int num;
Expand All @@ -14,6 +14,7 @@ int game_number_if_possible(const char * s, int red_asked, int green_asked, int
int red_in_tuple;
int green_in_tuple;
int blue_in_tuple;
int red_max = 0, green_max = 0, blue_max = 0;

sscanf(cur, "%d%n", &game_number, &bytes_read);
cur += bytes_read + 2; // skip the number of bytes for the game number and also ": "
Expand Down Expand Up @@ -53,15 +54,31 @@ int game_number_if_possible(const char * s, int red_asked, int green_asked, int

// printf("%d %d %d <=> %d %d %d\n", red_in_tuple, green_in_tuple, blue_in_tuple, red_asked, green_asked, blue_asked);

if ( (red_in_tuple > red_asked) || (green_in_tuple > green_asked) || (blue_in_tuple > blue_asked) )
if (red_in_tuple > red_max) {
red_max = red_in_tuple;
}
if (green_in_tuple > green_max) {
green_max = green_in_tuple;
}
if (blue_in_tuple > blue_max) {
blue_max = blue_in_tuple;
}

if ( mode == 1 )
{
return 0;
if ( (red_in_tuple > red_asked) || (green_in_tuple > green_asked) || (blue_in_tuple > blue_asked) )
{
return 0;
}
}
if ((*cur == '\0') || (*cur == '\n')) {
break;
}
}

// printf("set of cubes = %d %d %d\n", red_max, green_max, blue_max);
if ( mode == 2 ) return red_max * green_max * blue_max;

return game_number;
}

Expand Down Expand Up @@ -95,7 +112,7 @@ int main(int argc, const char* argv[]) {
if (p == NULL) {
break;
}
result += game_number_if_possible(s, red, green, blue);
result += game_output(s, red, green, blue, mode);
};

printf("Answer: %d\n", result);
Expand Down

0 comments on commit 78d7a92

Please sign in to comment.