aboutsummaryrefslogtreecommitdiff
path: root/2023/12/12a.c
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2023-12-12 19:13:24 +0100
committerSebastiano Tronto <sebastiano@tronto.net>2023-12-12 19:13:24 +0100
commit2d2b30491c4987e84b4b828a7705993aaa1bb5bb (patch)
treeb2ab4e9e3170984274f667e69715e977f6d2a283 /2023/12/12a.c
parent554ad47f09974f0131df285fda8ea50a1fdad8f5 (diff)
downloadaoc-2d2b30491c4987e84b4b828a7705993aaa1bb5bb.tar.gz
aoc-2d2b30491c4987e84b4b828a7705993aaa1bb5bb.zip
Added solution for 12
Diffstat (limited to '2023/12/12a.c')
-rw-r--r--2023/12/12a.c63
1 files changed, 63 insertions, 0 deletions
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 @@
1#include <inttypes.h>
2#include <stdbool.h>
3#include <stdio.h>
4#include <stdlib.h>
5#include <string.h>
6
7#define N 1000
8
9#define MAX(a,b) ((a)>(b)?(a):(b))
10#define MIN(a,b) ((a)<(b)?(a):(b))
11#define ABS(a) MAX((a),-(a))
12
13#define isnum(c) (c == '-' || (c >= '0' && c <= '9'))
14
15char *buf, line[N];
16int s, n, a[N];
17
18int readl(int nums[], char *buf) {
19 int64_t i;
20 for (i = 0; *buf; buf++) {
21 if (!isnum(*buf)) continue;
22 nums[i++] = atoll(buf);
23 while (isnum(*buf)) buf++;
24 }
25 return i;
26}
27
28int isgood(char *line, int a[], int n) {
29 int b[N];
30 memset(b, 0, N * sizeof(int));
31 memcpy(b, a, n * sizeof(int));
32 for (int j = 0, c = 0; *line != ' '; line++) {
33 if (*line == '?') printf("Error!\n");
34 if (*line == '.') { j += c; c = 0; }
35 if (*line == '#') { c = 1; b[j]--; }
36 }
37 for (int j = 0; j < N; j++)
38 if (b[j])
39 return 0;
40 return 1;
41}
42
43int arrange(char *line, char *buf, int a[], int n) {
44 while (*buf != '?' && *buf != ' ') buf++;
45 if (*buf == ' ')
46 return isgood(line, a, n);
47 *buf = '.';
48 int x = arrange(line, buf, a, n);
49 *buf = '#';
50 int y = arrange(line, buf, a, n);
51 *buf = '?';
52 return x + y;
53}
54
55int main() {
56 while ((buf = fgets(line, N, stdin)) != NULL) {
57 n = readl(a, buf);
58 s += arrange(line, buf, a, n);
59 }
60
61 printf("%d\n", s);
62 return 0;
63}

Generated with cgit - Back to sebastiano.tronto.net