Day 13: Point of Incidence

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/ , pastebin, or github (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


🔒 Thread is locked until there’s at least 100 2 star entries on the global leaderboard

🔓 Unlocked

  • @sjmulder
    link
    31 year ago

    C

    Implementing part 1 with a bunch of for loops made me wonder about elegant NumPy solutions but then part 2 came along and it was a perfect fit! Just change a flag to a counter and remove the if-match-early-exit.

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

    int main()
    {
    	static char g[32][32];
    	int p1=0,p2=0, w,h, x,y,i, nmis;
    	
    	while (!feof(stdin)) {
    		for (h=0; ; h++) {
    			assert(h < (int)LEN(*g));
    			if (!fgets(g[h], LEN(*g), stdin)) break;
    			if (!g[h][0] || g[h][0]=='\n') break;
    		}
    
    		assert(h>0); w = strlen(g[0])-1;
    		assert(w>0);
    
    		for (x=1; x<w; x++) {
    			nmis = 0;
    			for (i=0; i<x && x+i<w; i++)
    			for (y=0; y<h; y++)
    				nmis += g[y][x-i-1] != g[y][x+i];
    			if (nmis==0) p1 += x; else
    			if (nmis==1) p2 += x;
    		}
    
    		for (y=1; y<h; y++) {
    			nmis = 0;
    			for (i=0; i<y && y+i<h; i++)
    			for (x=0; x<w; x++)
    				nmis += g[y-i-1][x] != g[y+i][x];
    			if (nmis==0) p1 += y*100; else
    			if (nmis==1) p2 += y*100;
    		}
    	}
    
    	printf("13: %d %d\n", p1, p2);
    	return 0;
    }