this post was submitted on 08 Dec 2023
21 points (92.0% liked)

Advent Of Code

1093 readers
1 users here now

An unofficial home for the advent of code community on programming.dev!

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.

AoC 2024

Solution Threads

M T W T F S S
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25

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 8: Haunted Wasteland

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)
  • Code block support is not fully rolled out yet but likely will be in the middle of the event. Try to share solutions as both code blocks and using something such as https://topaz.github.io/paste/ , pastebin, or github (code blocks to future proof it for when 0.19 comes out and since code blocks currently function in some apps and some instances as well if they are running a 0.19 beta)

FAQ

you are viewing a single comment's thread
view the rest of the comments
[โ€“] sjmulder 2 points 2 years ago* (last edited 2 years ago)

C

Ough, that hurt. Didn't have good intuition for part 2. Made a guess that if the routes are cyclic, each with a fixed period and only visiting a terminal node once, I could work from there. Wrote some code to analyze the routes and was happy to see this was the case. Mucked about with a spreadsheet for too long before realizing it's just LCM.

GitHub link

Code (77 lines)

#include "common.h"

static char dirs[512];
static char names[1024][4];
static size_t map[1024][2];	/* left and right */
static size_t nnames; 

static ssize_t
get_name_idx(const char *name)
{
	size_t i;

	for (i=0; i<nnames; i++)
		if (!strcmp(names[i], name))
			return i;

	assert(nnames < LEN(names));
	snprintf(names[nnames], LEN(*names), "%s", name);
	return nnames++;
}

/* returns no. steps to first terminal */
static int
count_steps(size_t start, int part)
{
	size_t pos=start;
	int step=0;
	char *dir;

	for (dir=dirs; ; dir++, step++) {
		if ((!part && !strcmp(names[pos], "ZZZ")) ||
		    ( part && names[pos][2] == 'Z'))
			return step;
		if (*dir != 'R' && *dir != 'L')
			dir = dirs;
		pos = map[pos][*dir == 'R'];
	}
}

int
main(int argc, char **argv)
{
	char name[4], left[4], right[4];
	size_t pos;
	int64_t p1=0,p2=0, steps,inc;

	if (argc > 1)
		DISCARD(freopen(argv[1], "r", stdin));
	
	fgets(dirs, sizeof(dirs), stdin);

	while (scanf(" %3s = (%3s, %3s)", name, left, right) == 3) {
		pos = get_name_idx(name);
		map[pos][0] = get_name_idx(left);
		map[pos][1] = get_name_idx(right);
	}

	p1 = count_steps(get_name_idx("AAA"), 0);

	/*
	 * Part 2. Using LCM assuming that:
	 *  - all routes are cyclic
	 *  - exactly 1 terminal is visited each cycle
	 *  - period = first terminal, ie: terminals at 0(mod period)
	 */
	for (pos=0; pos<nnames; pos++)
		if (names[pos][2] == 'A') {
			steps = count_steps(pos, 1);
			if (!p2)
				p2 = steps;
			else for (inc = p2; p2 % steps; p2 += inc)
				;
		}

	printf("%"PRIi64" %"PRIi64"\n", p1, p2);
	return 0;
}