From 2d2b30491c4987e84b4b828a7705993aaa1bb5bb Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Tue, 12 Dec 2023 19:13:24 +0100 Subject: Added solution for 12 --- 2023/12/12a.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 2023/12/12a.c (limited to '2023/12/12a.c') diff --git a/2023/12/12a.c b/2023/12/12a.c new file mode 100644 index 0000000..d88ed9a --- /dev/null +++ b/2023/12/12a.c @@ -0,0 +1,63 @@ +#include +#include +#include +#include +#include + +#define N 1000 + +#define MAX(a,b) ((a)>(b)?(a):(b)) +#define MIN(a,b) ((a)<(b)?(a):(b)) +#define ABS(a) MAX((a),-(a)) + +#define isnum(c) (c == '-' || (c >= '0' && c <= '9')) + +char *buf, line[N]; +int s, n, a[N]; + +int readl(int nums[], char *buf) { + int64_t i; + for (i = 0; *buf; buf++) { + if (!isnum(*buf)) continue; + nums[i++] = atoll(buf); + while (isnum(*buf)) buf++; + } + return i; +} + +int isgood(char *line, int a[], int n) { + int b[N]; + memset(b, 0, N * sizeof(int)); + memcpy(b, a, n * sizeof(int)); + for (int j = 0, c = 0; *line != ' '; line++) { + if (*line == '?') printf("Error!\n"); + if (*line == '.') { j += c; c = 0; } + if (*line == '#') { c = 1; b[j]--; } + } + for (int j = 0; j < N; j++) + if (b[j]) + return 0; + return 1; +} + +int arrange(char *line, char *buf, int a[], int n) { + while (*buf != '?' && *buf != ' ') buf++; + if (*buf == ' ') + return isgood(line, a, n); + *buf = '.'; + int x = arrange(line, buf, a, n); + *buf = '#'; + int y = arrange(line, buf, a, n); + *buf = '?'; + return x + y; +} + +int main() { + while ((buf = fgets(line, N, stdin)) != NULL) { + n = readl(a, buf); + s += arrange(line, buf, a, n); + } + + printf("%d\n", s); + return 0; +} -- cgit v1.3