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
- What is this?: Here is a post with a large amount of details: https://programming.dev/post/6637268
- Where do I participate?: https://adventofcode.com/
- Is there a leaderboard for the community?: We have a programming.dev leaderboard with the info on how to join in this post: https://programming.dev/post/6631465
C
That did not go accordingly to plan! Took hours and a bunch of notepad paper 😅
Major mistakes:
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