aoc

Thing1's 2025 aoc
Log | Files | Refs

4a.c (758B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 
      5 char grid[150][150];
      6 
      7 int
      8 loadgrid(FILE *f) {
      9 	char line[150] = {0};
     10 	int i;
     11 	for (i = 0; fgets(line + 1, 149, f); i++) {
     12 		line[strlen(line + 1)] = 0;
     13 		memcpy(grid[i + 1], line, 150);
     14 	}
     15 	return i;
     16 }
     17 
     18 int
     19 countadjacent(int x, int y) {
     20 	int adjacent = 0;
     21 	for (int i = -1; i < 2; i++) {
     22 		for (int j = -1; j < 2; j++) {
     23 			if (grid[i  + y + 1][j + x + 1] == '@')
     24 				adjacent++;
     25 		}
     26 	}
     27 	return adjacent - 1;
     28 }
     29 
     30 int
     31 main() {
     32 	FILE *in = fopen("input", "r");
     33 	int lines = loadgrid(in);
     34 	int total = 0;
     35 
     36 	for (int y = 0; y < lines + 1; y++) {
     37 		for (int x = 0; x < strlen(grid[y] + 1); x++) {
     38 			if (grid[y][x + 1] == '@' && countadjacent(x, y - 1) < 4)
     39 				total++;
     40 		}
     41 	}
     42 	printf("%d\n", total);
     43 
     44 }