advent2025

Advent of Code 2025 Solutions
git clone git://bsandro.tech/advent2025
Log | Files | Refs | LICENSE

commit b882276f2387d250e46e6d88919c513aaf3bcbf3
parent 82d22cf2f4e37acc077b8a2f630e68c7a4f72933
Author: bsandro <email@bsandro.tech>
Date:   Thu,  4 Dec 2025 08:41:37 +0200

day04

Diffstat:
Aday04.c | 68++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 68 insertions(+), 0 deletions(-)

diff --git a/day04.c b/day04.c @@ -0,0 +1,68 @@ +#include <stdio.h> +#include "cputime.h" + +// +1 on all sides +#define W 141 +#define H 141 + +#define LEN(x) (sizeof((x))/sizeof(*(x))) + +typedef char Map[H][W]; +typedef struct { + int x, y; +} Point; + +int count_adj(Map map, int x, int y) { + int cnt = 0; + Point points[] = { + {x-1,y+1}, {x,y+1}, {x+1,y+1}, + {x-1,y }, {x+1,y }, + {x-1,y-1}, {x,y-1}, {x+1,y-1} + }; + for (size_t p=0;p<LEN(points);++p) { + if (map[points[p].y][points[p].x]=='@') cnt++; + } + return cnt; +} + +int clean(Map map) { + int rem = 0; + for (int y=1;y<H-1;++y) { + for (int x=1;x<W-1;++x) { + if (map[y][x]=='@'&&count_adj(map, x, y)<4) { + map[y][x]='.'; + rem++; + } + } + } + + return rem; +} + +int main(void) { + Map map = {0}; + int x = 1; + int y = 1; + for (int c=getchar();c!=EOF;c=getchar()) { + if (c!='\n') map[y][x++] = c; + else { + y++; + x=1; + } + } + + int p1 = 0; + for (int y=1;y<H-1;++y) { + for (int x=1;x<W-1;++x) { + if (map[y][x]=='@'&&count_adj(map, x, y)<4) p1++; + } + } + int p2 = 0; + while (1) { + int rem = clean(map); + p2 += rem; + if (rem==0) break; + } + printf("p1: %d\np2: %d\n", p1, p2); + return 0; +}