Day 2: Cube Conundrum


Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • Code block support is not fully rolled out yet but likely will be in the middle of the event. Try to share solutions as both code blocks and using something such as https://topaz.github.io/paste/ or pastebin (code blocks to future proof it for when 0.19 comes out and since code blocks currently function in some apps and some instances as well if they are running a 0.19 beta)

FAQ


🔒This post will be unlocked when there is a decent amount of submissions on the leaderboard to avoid cheating for top spots

🔓 Edit: Post has been unlocked after 6 minutes

  • sjmulder@lemmy.sdf.org
    link
    fedilink
    arrow-up
    3
    ·
    edit-2
    7 months ago

    String parsing! Always fun in C!

    https://github.com/sjmulder/aoc/blob/master/2023/c/day02.c

    int main(int argc, char **argv)
    {
    	char ln[256], *sr,*srd,*s;
    	int p1=0,p2=0, id, r,g,b;
    
    	for (id=1; (sr = fgets(ln, sizeof(ln), stdin)); id++) {
    		strsep(&sr, ":");
    		r = g = b = 0;
    
    		while ((srd = strsep(&sr, ";")))
    		while ((s = strsep(&srd, ",")))
    			if (strchr(s, 'd')) r = MAX(r, atoi(s)); else
    			if (strchr(s, 'g')) g = MAX(g, atoi(s)); else
    			if (strchr(s, 'b')) b = MAX(b, atoi(s));
    	
    		p1 += (r <= 12 && g <= 13 && b <= 14) * id;
    		p2 += r * g * b;
    	}
    
    	printf("%d %d\n", p1, p2);
    	return 0;
    }