Day 19: Aplenty
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
Bit of typing and testing again but part 2 was fun and not hard, although I did make a few logic mistakes.
Didn’t optimize anything in particular but as in day 8 (iirc) I avoid dealing with strings for references to things, so I keep a string table and only use indices.
Abridged excerpt of data structures and
eval()
:struct expr { int type, var, imm, next; }; struct partrange { int min[4], max[4]; }; static struct expr flows[600][5]; static int accept_id, reject_id, in_id; static int64_t eval(int id, struct partrange p) { struct partrange q; struct expr *e; int64_t acc=0; int i; if (id == reject_id || p.min[0] > p.max[0] || p.min[1] > p.max[1] || p.min[2] > p.max[2] || p.min[3] > p.max[3]) return 0; if (id == accept_id) return (int64_t) (p.max[0] -p.min[0] +1) * (p.max[1] -p.min[1] +1) * (p.max[2] -p.min[2] +1) * (p.max[3] -p.min[3] +1); for (i=0; i < (int)LEN(*flows); i++) switch ((e = &flows[id][i])->type) { case EXPR_LT: q = p; q.max[e->var] = MIN(q.max[e->var], e->imm-1); p.min[e->var] = MAX(p.min[e->var], e->imm); acc += eval(e->next, q); break; case EXPR_GT: q = p; q.min[e->var] = MAX(q.min[e->var], e->imm+1); p.max[e->var] = MIN(p.max[e->var], e->imm); acc += eval(e->next, q); break; case EXPR_CALL: acc += eval(e->next, p); return acc; } assert(!"bad flow"); }
https://github.com/sjmulder/aoc/blob/master/2023/c/day19.c