this post was submitted on 19 Dec 2023
6 points (100.0% liked)

Advent Of Code

1227 readers
2 users here now

An unofficial home for the advent of code community on programming.dev! Other challenges are also welcome!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

Everybody Codes is another collection of programming puzzles with seasonal events.

EC 2025

AoC 2025

Solution Threads

M T W T F S S
1 2 3 4 5 6 7
8 9 10 11 12

Visualisations Megathread

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 2 years ago
MODERATORS
 

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

you are viewing a single comment's thread
view the rest of the comments
[–] sjmulder 2 points 2 years ago

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