this post was submitted on 04 Dec 2024
18 points (95.0% liked)

Advent Of Code

996 readers
4 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 4: Ceres Search

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 1 month ago

C

What can I say, bunch of for loops! I add a 3 cell border to avoid having to do bounds checking in the inner loops.

Code

#include "common.h"
#define GZ 146

int main(int argc, char **argv) {
	static char g[GZ][GZ];
	static const char w[] = "XMAS";
	int p1=0,p2=0, x,y, m,i;

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

	for (y=3; y<GZ-3; y++)
	for (x=3; x<GZ-3; x++) {
		for (m=1,i=0; i<4; i++) {m &= g[y+i][x]==w[i];} p1+=m;
		for (m=1,i=0; i<4; i++) {m &= g[y-i][x]==w[i];} p1+=m;
		for (m=1,i=0; i<4; i++) {m &= g[y][x+i]==w[i];} p1+=m;
		for (m=1,i=0; i<4; i++) {m &= g[y][x-i]==w[i];} p1+=m;
		for (m=1,i=0; i<4; i++) {m &= g[y+i][x+i]==w[i];} p1+=m;
		for (m=1,i=0; i<4; i++) {m &= g[y-i][x-i]==w[i];} p1+=m;
		for (m=1,i=0; i<4; i++) {m &= g[y+i][x-i]==w[i];} p1+=m;
		for (m=1,i=0; i<4; i++) {m &= g[y-i][x+i]==w[i];} p1+=m;

		p2 += g[y+1][x+1]=='A' &&
		      ((g[y][x]  =='M' && g[y+2][x+2]=='S')  ||
		       (g[y][x]  =='S' && g[y+2][x+2]=='M')) &&
		      ((g[y+2][x]=='M' && g[y][x+2]  =='S')  ||
		       (g[y+2][x]=='S' && g[y][x+2]  =='M'));
	}

	printf("04: %d %d\n", p1, p2);
}

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