commit 8537cc91d8cc26a533756e266225c1bbbbb5bc8e
parent 2f3120f553b9ee89bb83306ebd90b0d71684e644
Author: bsandro <email@bsandro.tech>
Date: Sat, 2 Dec 2023 08:24:09 +0200
day 02 p2 still no brain cells involved
Diffstat:
1 file changed, 18 insertions(+), 9 deletions(-)
diff --git a/day02/puzzle.c b/day02/puzzle.c
@@ -1,5 +1,6 @@
#define _DEFAULT_SOURCE
+#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
@@ -31,7 +32,7 @@ void puzzle(const char *filename, long long *result1, long long *result2) {
int game_num;
int n = sscanf(buf, "Game %d:", &game_num);
if (n==1) {
- printf("game %d\n", game_num);
+ //printf("game %d\n", game_num);
} else {
printf("parse error\n");
break;
@@ -46,6 +47,7 @@ void puzzle(const char *filename, long long *result1, long long *result2) {
bool game_valid = true;
int len = strlen(game_sep);
int i = 0;
+ int r=0,g=0,b=0;
while (i<len) {
if (is_digit(game_sep[i])) {
int number = game_sep[i]-'0';
@@ -54,14 +56,19 @@ void puzzle(const char *filename, long long *result1, long long *result2) {
++i;
}
char color = game_sep[i+2];
- printf("%d %c\n", number, color);
- if ((color=='r' && number>12) ||
- (color=='g' && number>13) ||
- (color=='b' && number>14))
- {
- printf("invalid game");
- game_valid = false;
- break;
+ //printf("%d %c\n", number, color);
+
+ if (color=='r') {
+ if (number>12) game_valid = false;
+ if (number>r) r = number;
+ }
+ if (color=='g') {
+ if (number>13) game_valid = false;
+ if (number>g) g = number;
+ }
+ if (color=='b') {
+ if (number>14) game_valid = false;
+ if (number>b) b = number;
}
}
++i;
@@ -71,6 +78,8 @@ void puzzle(const char *filename, long long *result1, long long *result2) {
*result1 += game_num;
}
+ *result2 += r*g*b;
+
++line_num;
bzero(buf, STR_LEN);
}