summary refs log tree commit diff homepage
path: root/2023/day03.c
blob: 4b9169593197b4a0109a7f10f86ac27b4f7319f8 (plain) (blame)
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
26
27
28
29
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
	int sum = 0, sum2 = 0;
	char g[150][150];
	int h = 0;
	while (gets(g[h++]));
	int w = strlen(g[--h-1]);
	for (int y = 0; y < h; ++y)
	for (int x = 0; x < w; ++x) {
		if (g[y][x] == '.' || isdigit(g[y][x])) continue;
		int n = 0, r = 1;
		for (int i = -1; i <= +1; ++i)
		for (int j = -1; j <= +1; ++j) {
			char *p = &g[y+i][x+j];
			if (!isdigit(*p)) continue;
			while (p > g[y+i] && isdigit(p[-1])) p--;
			sum += atoi(p);
			r *= atoi(p);
			n++;
			while (isdigit(*p)) *p++ = '.';
		}
		if (n == 2) sum2 += r;
	}
	printf("%d\n", sum);
	printf("%d\n", sum2);
}