Day 14: Parabolic Reflector Dish

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

Edit: 🔓 Unlocked

  • @sjmulder
    link
    4
    edit-2
    6 months ago

    C

    Chose not to do transposing/flipping or fancy indexing so it’s rather verbose, but it’s also clear and (I think) fast. I also tried to limit the number of search steps by keeping two cursors in the current row/col, rather than shooting a ray every time.

    Part 2 immediately reminded me of that Tetris puzzle from day 22 last year so I knew how to find and apply the solution. State hashes are stored in an array and (inefficiently) scanned until a loop is found.

    One direction of the shift function:

    /*
     * Walk two cursors i and j through each column x. The i cursor
     * looks for the first . where an O can go. The j cursor looks
     * ahead for O's. When j finds a # we start again beyond it.
     */
    for (x=0; x<w; x++)
    for (i=0, j=1; i<h && j<h; )
    	if (j <= i) j = i+1;
    	else if (g[j][x] == '#') i = j+1;
    	else if (g[j][x] != 'O') j++;
    	else if (g[i][x] != '.') i++;
    	else { g[i++][x] = 'O'; g[j++][x] = '.'; vis14_emit(); }
    

    The main loop:

    for (nleft = 1*1000*1000*1000; nleft; nleft--) {
    	shift_all();
    
    	if (!period) {
    		assert(nhist < (int)LEN(hist));		
    		hist[nhist++] = hash_grid();
    
    		for (i=0; i<nhist-1; i++)
    			if (hist[i] == hist[nhist-1]) {
    				period = nhist-1 - i;
    				nleft = nleft % period;
    				break;
    			}
    	}
    }
    

    Full solution: https://github.com/sjmulder/aoc/blob/master/2023/c/day14.c