Skip to content

Instantly share code, notes, and snippets.

@zid
Created December 2, 2023 17:43
Show Gist options
  • Save zid/eb98f6676be445b1e6f90ac03cac0611 to your computer and use it in GitHub Desktop.
Save zid/eb98f6676be445b1e6f90ac03cac0611 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(void)
{
FILE *f;
unsigned long total = 0;
f = fopen(__FILE__ ".txt", "rb");
if(!f)
{
fprintf(stderr, "idiot\n");
return 0;
}
while(1)
{
char buf[256];
int i, game, red, green, blue;
char *p, *s;
if(!fgets(buf, 256, f))
break;
if(buf[0] == '\n' || buf[0] == '\r')
break;
*strchr(buf, '\r') = 0;
if(memcmp(buf, "Game ", 5) != 0)
break;
p = buf + 5;
game = strtol(p, &p, 10);
p += 2;
i = strtol(strtok(p, ",; "), NULL, 10);
p = strtok(NULL, ",; ");
red = 0;
green = 0;
blue = 0;
while(1)
{
if(memcmp(p, "red", 3) == 0 && i > red)
red = i;
if(memcmp(p, "green", 5) == 0 && i > green)
green = i;
if(memcmp(p, "blue", 4) == 0 && i > blue)
blue = i;
p = strtok(NULL, ",; ");
if(!p)
break;
i = strtol(p, NULL, 10);
p = strtok(NULL, ",; ");
if(!p)
break;
}
printf("Game %d, RGB: %d %d %d\n", game, red, green, blue);
total += (red * green * blue);
}
printf("\nTotal: %lu\n", total);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment