Day 21: Step

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)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

FAQ

  • sjmulder
    link
    fedilink
    arrow-up
    1
    ·
    edit-2
    1 year ago

    C

    That did not go accordingly to plan! Took hours and a bunch of notepad paper 😅

    Major mistakes:

    • Off by one reading input so I was missing a row.
    • Messing up the area calculations for certain cases, like double-counting some diagonals or not counting them. Suppose those are off by ones too.
    • Not realizing the odd grid size meant the tick/tock states would alternate between repetitions of the grid within the covered area.

    Some code:

    static char map[GSZ][GSZ];	/* all reachable replaced by \0 */
    static int nobs[2][2];		/* [in/out diamond][even/odd t] */
    static int sz;
    
    /*
     * We call the big diamond in the input (covered by part 1) the 'inside 
     * diamond' and the diamond found by combining the corner pieces on the
     * 'outside diamond'. For both we found the number of unreachable tiles
     * (obstacles, 'nobs') for the even and odd time steps.
     *
     * The total number of tiles covered in t is (t+1)^2 - accounting for
     * the checkerboard reachability pattern. With the part 1 and 2
     * questions, this exactly covers a certain square amount of diamonds
     * (ndia*ndia).
     *
     * Finding out which types of diamonds and in what state (odd/even) is
     * the tricky bit, e.g. the state alternates between the repeating grids
     * because the grid size is uneven.
     */
    static uint64_t find_reach(uint64_t t)
    {
    	int odd = t&1;
    	uint64_t ndia = t/sz*2 +1;
    
    	return (t+1)*(t+1) -
    	    (ndia/2 +!odd) * (ndia/2 +!odd) * nobs[0][0] -
    	    (ndia/2 + odd) * (ndia/2 + odd) * nobs[0][1] -
    	    ndia*ndia/4 * nobs[1][0] -
    	    ndia*ndia/4 * nobs[1][1];
    }
    

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