this post was submitted on 08 Dec 2024
23 points (96.0% liked)

Advent Of Code

994 readers
3 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 1 year ago
MODERATORS
 

Day 8: Resonant Collinearity

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
[โ€“] [email protected] 2 points 3 weeks ago

C

Not hard but a little fiddly.

Code

#include "common.h"

#define GZ 52

static char g[GZ][GZ];
#define ANTI_P1 1
#define ANTI_P2 2
static uint8_t anti[GZ][GZ];
static int w,h;

int
main(int argc, char **argv)
{
	int p1=0,p2=0, x,y, x1,y1, ax,ay, i;
	char *lf;

	if (argc > 1)
		DISCARD(freopen(argv[1], "r", stdin));
	for (h=0; h<GZ && fgets(g[h], GZ, stdin); h++)
		;

	assert(feof(stdin));
	lf = strchr(g[0], '\n');
	assert(lf);
	w = lf - g[0];

	/*
	 * Find antenna pairs, then project backwards from the first,
	 * forwards from the second. Don't like the repetition but it
	 * makes for easy code.
	 */
	for (y=0; y<h; y++)
	for (x=0; x<w; x++) {
		if (!isalnum(g[y][x]))
			continue;

		for (y1=y; y1<h; y1++)
		for (x1=(y==y1?x+1:0); x1<w; x1++) {
			if (g[y][x] != g[y1][x1])
				continue;

			for (i=0; ; i++) {
				if ((ax = x-(x1-x)*i) <0 || ax>w ||
				    (ay = y-(y1-y)*i) <0 || ay>h)
					break;
				anti[ay][ax] |= ANTI_P1 * i==1;
				anti[ay][ax] |= ANTI_P2;
			}

			for (i=0; ; i++) {
				if ((ax = x1+(x1-x)*i) <0 || ax>w ||
				    (ay = y1+(y1-y)*i) <0 || ay>h)
					break;
				anti[ay][ax] |= ANTI_P1 * i==1;
				anti[ay][ax] |= ANTI_P2;
			}
		}
	}

	for (y=0; y<h; y++)
	for (x=0; x<w; x++) {
		p1 += !!(anti[y][x] & ANTI_P1);
		p2 += !!(anti[y][x] & ANTI_P2);
	}

	printf("08: %d %d\n", p1, p2);
	return 0;
}

https://github.com/sjmulder/aoc/blob/master/2024/c/day08.c