Day 3: Gear Ratios


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 11 minutes

  • sjmulder@lemmy.sdf.org
    link
    fedilink
    arrow-up
    2
    ·
    edit-2
    1 year ago

    Language: C

    Part 2 stumped me for a little bit, it wasn’t an obvious extension of part 1. Part 1 was about numbers (with one or more …) while part 2 worked from the symbols (with exactly two …). Going the other way would require more bookkeeping to avoid double counting.

    And for the implementation: if you loop over the grid and check surrounding cells for digits you’d have to account for a bunch of cases, e.g. NW/N or N/NE being part of the same number or NW and NE being part of separate numbers. And you’d have to parse the numbers again. But building a graph or reference list of some sort is both unergonomic with C and not necessarily any simpler.

    I ended up just writing out the cases, and honestly it didn’t turn out too bad.

    GitHub link

    Abridged code
    int main(int argc, char **argv)
    {
    	static char G[GSZ][GSZ];
    	static int N[GSZ][GSZ];
    	int p1=0,p2=0, h=0, x,y, dx,dy, n=0,sym=0,r;
    	
    	for (h=0; fgets(&G[h+1][1], GSZ-1, stdin); h++)
    		assert(h < GSZ);
    
    	/*
    	 * Pass 1: parse numbers and solve part 1. For every digit in
    	 * G, the full number it is part of is stored in N.
    	 */
    	for (y=1; y<=h; y++)
    	for (x=1; G[y][x]; x++)
    		if (isdigit(G[y][x])) {
    			n = n*10 + G[y][x]-'0';
    
    			for (dy=-1; dy<2; dy++)
    			for (dx=-1; dx<2; dx++)
    				sym = sym || (x && y &&
    				    G[y+dy][x+dx] != '.' &&
    				    ispunct(G[y+dy][x+dx]));
    		} else {
    			for (dx=-1; isdigit(G[y][x+dx]); dx--)
    				N[y][x+dx] = n;
    			if (sym)
    				p1 += n;
    			n = sym = 0;
    		}
    
    	/*
    	 * Pass 2: solve part 2 by finding all '*', then counting and
    	 * multiplying adjecent numbers.
    	 *
    	 * Horizontal adjecency is trivial but vertical/diagonal has
    	 * two situations: if there's a digit directly North of the '+',
    	 * it must be a single number: NW and NE would connect to it.
    	 * If N isn't a digit, digits in NW and NE belong to separate
    	 * numbers.
    	 */
    	for (y=1; y<=h; y++)
    	for (x=1; G[y][x]; x++) {
    		if (G[y][x] != '*')
    			continue;
    
    		n = 0; r = 1;
    
    		if (N[y][x-1]) { n++; r *= N[y][x-1]; }
    		if (N[y][x+1]) { n++; r *= N[y][x+1]; }
    
    		if (N[y-1][x]) { n++; r *= N[y-1][x]; } else {
    			if (N[y-1][x-1]) { n++; r *= N[y-1][x-1]; }
    			if (N[y-1][x+1]) { n++; r *= N[y-1][x+1]; }
    		}
    
    		if (N[y+1][x]) { n++; r *= N[y+1][x]; } else {
    			if (N[y+1][x-1]) { n++; r *= N[y+1][x-1]; }
    			if (N[y+1][x+1]) { n++; r *= N[y+1][x+1]; }
    		}
    
    		if (n == 2)
    			p2 += r;
    	}
    
    	printf("%d %d\n", p1, p2);
    	return 0;
    }