Day 19 - Linen Layout
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
Interestingly part 1 already defied a naive approach. It was fun thinking of a way to memoize without hash tables.
Code
#include "common.h" static char *pats[480]; static int lens[480]; int np; /* memoized for 's' by mem[off], 0 = unknown, >0 = value+1 */ static int64_t recur(char *s, int off, int64_t *mem) { int64_t acc=0; int i; if (!s[off]) return 1; if (mem[off]) return mem[off]-1; for (i=0; i<np; i++) if (!strncmp(s+off, pats[i], lens[i])) acc += recur(s, off+lens[i], mem); mem[off] = acc+1; return acc; } int main(int argc, char **argv) { static char patbuf[3200], design[64]; int64_t p1=0,p2=0, mem[64], n; char *rest, *lf; if (argc > 1) DISCARD(freopen(argv[1], "r", stdin)); rest = fgets(patbuf, sizeof(patbuf), stdin); for (; (pats[np] = strsep(&rest, ",")); np++) { while (isspace(pats[np][0])) pats[np]++; /* skip spaces */ if ((lf = strchr(pats[np], '\n'))) *lf = '\0'; /* trim trailing \n */ lens[np] = strlen(pats[np]); assert(np+1 < (int)LEN(pats)); } while (scanf(" %63s", design) == 1) { memset(mem, 0, sizeof(mem)); n = recur(design, 0, mem); p1 += n >0; p2 += n; } printf("19: %"PRId64" %"PRId64"\n", p1, p2); return 0; }
https://codeberg.org/sjmulder/aoc/src/branch/master/2024/c/day19.c
Zee
Also a port to my cursed Dutch dialect of C, Zee:
Code
https://codeberg.org/sjmulder/aoc/src/