aboutsummaryrefslogtreecommitdiff
path: root/2023/03/3b.c
blob: 9bf4c277bebc061812963e1685835ed638b22dda (plain)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N 1000
#define ISNUM(c) (c >= '0' && c <= '9')

int grab(char *buf) {
	if (!ISNUM(*buf)) return 0;
	while (ISNUM(*buf)) buf--;
	buf++;
	int x = atoi(buf);
	while (ISNUM(*buf)) *buf++ = '.';

	return x;
}

void push(int *j, int *g, char *buf) {
	g[*j] = grab(buf);
	if (g[*j] > 0) (*j)++;
}

int main() {
	char *cont, line[N], prev[N], next[N], backup[3][N];
	int i, sum, j, g[9];

	sum = 0;
	memset(line, '.', N);
	memset(prev, '.', N);
	memset(next, '.', N);
	cont = fgets(&next[1], N-1, stdin);
	while (cont != NULL) {
		memcpy(line, next, N);
		cont = fgets(&next[1], N-1, stdin);
		for (i = 1; line[i]; i++) {
			if (line[i] == '*') {
				memcpy(backup[0], prev, N);
				memcpy(backup[1], line, N);
				memcpy(backup[2], next, N);
				j = 0;
				push(&j, g, &prev[i-1]);
				push(&j, g, &prev[i]);
				push(&j, g, &prev[i+1]);
				push(&j, g, &line[i-1]);
				push(&j, g, &line[i+1]);
				push(&j, g, &next[i-1]);
				push(&j, g, &next[i]);
				push(&j, g, &next[i+1]);
				if (j == 2) sum += g[0] * g[1];
				memcpy(prev, backup[0], N);
				memcpy(line, backup[1], N);
				memcpy(next, backup[2], N);
			}
		}
		memcpy(prev, line, N);
	}

	printf("%d\n", sum);
	return 0;
}

Generated with cgit - Back to sebastiano.tronto.net