72 lines
2.1 KiB
C
72 lines
2.1 KiB
C
|
#include <stdio.h>
|
||
|
#include <string.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <stdbool.h>
|
||
|
|
||
|
#define LINELENGTH 150
|
||
|
#define NEXT(X) ((X + 3 + 1) % 3)
|
||
|
#define PREV(X) ((X + 3 - 1) % 3)
|
||
|
|
||
|
/* I wish I had python generator or Go coroutine... */
|
||
|
#define STATE_BEGIN 0
|
||
|
#define STATE_READING 1
|
||
|
#define STATE_END 2
|
||
|
int read_state = STATE_BEGIN;
|
||
|
|
||
|
/* Return at least 3 lines; first and last lines and first chars are all '.' */
|
||
|
char* readline(char* buf, int max_size) {
|
||
|
switch (read_state) {
|
||
|
case STATE_BEGIN:
|
||
|
memset(buf, '.', max_size);
|
||
|
buf[max_size - 2] = '\n';
|
||
|
buf[max_size - 1] = '\0';
|
||
|
read_state = STATE_READING;
|
||
|
return buf;
|
||
|
case STATE_READING:
|
||
|
buf[0] = '.';
|
||
|
if (!fgets(buf + 1, max_size, stdin)) {
|
||
|
memset(buf, '.', max_size);
|
||
|
buf[max_size - 2] = '\n';
|
||
|
buf[max_size - 1] = '\0';
|
||
|
read_state = STATE_END;
|
||
|
}
|
||
|
return buf;
|
||
|
case STATE_END:
|
||
|
return NULL;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#define IS_SYMBOL(X) ((X < '0' || X > '9') && X != '.' && X != '\n')
|
||
|
#define IS_ADJACENT_SYMBOL(buf, i, j) ( \
|
||
|
IS_SYMBOL(buf[i][j - 1]) || IS_SYMBOL(buf[i][j + 1]) || \
|
||
|
IS_SYMBOL(buf[PREV(i)][j]) || IS_SYMBOL(buf[NEXT(i)][j]) || \
|
||
|
IS_SYMBOL(buf[PREV(i)][j - 1]) || IS_SYMBOL(buf[PREV(i)][j + 1]) || \
|
||
|
IS_SYMBOL(buf[NEXT(i)][j - 1]) || IS_SYMBOL(buf[NEXT(i)][j + 1]))
|
||
|
|
||
|
int main(void) {
|
||
|
char buf[3][LINELENGTH];
|
||
|
int sum = 0;
|
||
|
|
||
|
readline(buf[0], LINELENGTH);
|
||
|
readline(buf[1], LINELENGTH);
|
||
|
for (int i = 1; readline(buf[NEXT(i)], LINELENGTH); i = NEXT(i)) {
|
||
|
bool is_part = false;
|
||
|
int num = 0;
|
||
|
|
||
|
for (int j = 1; buf[i][j]; j++) { // j = 0 is '.' guard
|
||
|
if (buf[i][j] >= '0' && buf[i][j] <= '9') {
|
||
|
num = num * 10 + (buf[i][j] - '0');
|
||
|
is_part = is_part || IS_ADJACENT_SYMBOL(buf, i, j);
|
||
|
} else {
|
||
|
sum += is_part ? num : 0;
|
||
|
is_part = false;
|
||
|
num = 0;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
printf("%d\n", sum);
|
||
|
return 0;
|
||
|
}
|
||
|
|