Day 21: Keypad Conundrum
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
Finally got this one done very late last night!
I kept getting stuck reasoning about the recursion here. For some reason I got myself convinced that after a move, the state of the ‘upper’ dpads could make it more advantageous to pick one followup move over another - i.e. steps aren’t independent.
It took a bunch of manually working through sequences to convince myself that, after every move, every dpad above it would be on A. With that, it’s ‘just’ recursive pathfinding for independent moves.
Since there are relatively few types of moves needed on the dpad, I just sketched them out and wrote the routes in code directly (up to two options per move, e.g. left,up or up,left).
Code
#include "common.h" static int64_t dpmem[26][8][2]; enum {NW,NN,NE,EE,SE,SS,SW,WW}; /* * We can sufficiently describe npad/dpad movements as: "move N x, N y, * then press A N times" (a 'move'). It's never faster to take a detour. * After pressing A on one dpad, dpads on all levels above will also be * on A, so these moves can be considered in isolation. * * This function gives the cost of executing a move with the dpad in * direction 'd' at level 'l', excluding the A presses for actually * tapping the selected directions, but including returning the cursor * back to the A button. * * E.g., moving 2 up and 3 left (NW), the steps could be <AAv<AAA>>^. * Excluding the A presses, that's 6 steps. Pulling out the A presses * lets us drop the dx and dy arguments and simplify the memoization. * They're easily calculated: it's |dx|+|dy|. * * The gaps present a problem: picking the lowest-cost option (e.g. * left-then-up vs. up-then-left) may cause us to cross a gap. The * "mind the gap" argument 'mtg' must be set to 1 if-and-only-if there * is such potential, e.g. a move from < to ^ on the dpad (up,right * crosses the gap) or a move from A to 1 on the npad (left,up crosses * the gap). With the flag set, these orderings will be avoided. */ static int64_t dp(int d, int mtg, int l) { int64_t ret, alt=INT64_MAX; if (l<=0) return 0; assert(l >= 0); assert(l < (int)LEN(dpmem)); assert(mtg==0 || mtg==1); if ((ret = dpmem[l][d][mtg])) return ret; /* routes avoiding gaps where necessary */ ret = d==SE ? 1+dp(SS,0,l-1) + 1+dp(WW,0,l-1) + 2+dp(SE,0,l-1) : d==SW ? 2+dp(SW,0,l-1) + 1+dp(WW,0,l-1) + 3+dp(NE,1,l-1) : d==SS ? 2+dp(SW,0,l-1) + 2+dp(NE,0,l-1) : d==NE ? 1+dp(SS,0,l-1) + 2+dp(NW,0,l-1) + 1+dp(EE,0,l-1) : d==NW ? 1+dp(WW,0,l-1) + 2+dp(SW,1,l-1) + 3+dp(NE,1,l-1) : d==NN ? 1+dp(WW,0,l-1) + 1+dp(EE,0,l-1) : d==EE ? 1+dp(SS,0,l-1) + 1+dp(NN,0,l-1) : d==WW ? 3+dp(SW,1,l-1) + 3+dp(NE,1,l-1) : (assert(!"bad dir"), -1); /* alternatives crossing the gaps */ alt = mtg ? INT64_MAX : d==SE ? 2+dp(SW,0,l-1) + 1+dp(EE,0,l-1) + 1+dp(NN,0,l-1) : d==SW ? 3+dp(SW,1,l-1) + 1+dp(EE,0,l-1) + 2+dp(NE,0,l-1) : d==NE ? 1+dp(WW,0,l-1) + 2+dp(SE,0,l-1) + 1+dp(NN,0,l-1) : d==NW ? 3+dp(SW,1,l-1) + 2+dp(NE,1,l-1) + 1+dp(EE,0,l-1) : INT64_MAX; return dpmem[l][d][mtg] = MIN(ret, alt); } static int64_t npcost(const char *s, int lv) { int64_t cost=0; int x=2,y=3, x1,y1, mtg, dir; for (; *s == 'A' || (*s >= '0' && *s <= '9'); s++) { x1 = *s=='A' ? 2 : *s=='0' ? 1 : 2-(9-*s+'0') % 3; y1 = *s=='A' ? 3 : *s=='0' ? 3 : (9-*s+'0') / 3; /* potentially crossing a gap? */ mtg = (x==0 && y1==3) || (x1==0 && y==3); dir = y1>y ? (x1>x ? SE : x1<x ? SW : SS) : y1<y ? (x1>x ? NE : x1<x ? NW : NN) : (x1>x ? EE : x1<x ? WW : 0); cost += dp(dir, mtg, lv) + abs(x1-x) + abs(y1-y) +1; x = x1; y = y1; } return cost; } int main(int argc, char **argv) { char buf[8]; int digits; int64_t p1=0,p2=0; if (argc > 1) DISCARD(freopen(argv[1], "r", stdin)); while (fgets(buf, sizeof(buf), stdin)) { digits = atoi(buf); p1 += digits * npcost(buf, 2); p2 += digits * npcost(buf, 25); } printf("21: %"PRId64" %"PRId64"\n", p1, p2); return 0; }
https://codeberg.org/sjmulder/aoc/src/branch/master/2024/c/day21.c