-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path19.c
197 lines (178 loc) · 4.82 KB
/
19.c
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include <assert.h>
#include <ctype.h>
#include <err.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct rule {
char ch;
bool is_char;
int32_t main[3];
int32_t nmain;
int32_t alt[3];
int32_t nalt;
int32_t index;
};
typedef struct rule rule_t;
static int32_t
message_matches_rule(const rule_t* restrict rule,
const rule_t* restrict rules,
char* restrict m,
const int32_t depth) {
if (rule->is_char) {
return *m == rule->ch ? 1 : 0;
}
// first, try main branch
int32_t characters_matched = 0;
int32_t inc;
int32_t r;
char* tmp = m;
int32_t rid;
for (r = 0; r < rule->nmain; r++) {
rid = rule->main[r];
const rule_t* main = &rules[rid];
inc = message_matches_rule(main, rules, m, depth + 1);
if (inc == 0) {
break;
} else {
m += inc;
characters_matched += inc;
// detect recursion for rules 31 and 42...
if (r > 0 && (main->index == 31 || main->index == 42) && *m == '\n') {
r = rule->nmain;
break;
}
}
}
// if main failed, check against alt branch
if (r < rule->nmain) {
m = tmp;
characters_matched = 0;
for (r = 0; r < rule->nalt; r++) {
rid = rule->alt[r];
const rule_t* alt = &rules[rid];
inc = message_matches_rule(alt, rules, m, depth + 1);
if (inc == 0) {
// detect recursion for rules 31 and 42...
if (r > 0 && (alt->index == 31 || alt->index == 42)) {
r = rule->nalt;
break;
}
return 0;
} else {
m += inc;
characters_matched += inc;
}
}
if (r < rule->nalt) {
return 0;
}
}
if (rule->index == 0 && *m != '\n' && *m != '\0') {
return 0;
}
// for (int i=0; i < depth; i++) printf(" |");
// printf("OK! %d characters checked.\n", characters_matched);
return characters_matched;
}
int day19(void) {
FILE* f = fopen("inputs/19.txt", "r");
if (!f) {
err(EXIT_FAILURE, "error reading input file");
}
char linebuf[BUFSIZ] = {0};
// parse rules
rule_t rules[256];
for (int32_t i = 0; i < 200; i++) {
rules[i].nmain = 0;
rules[i].nalt = 0;
rules[i].index = 0;
for (int8_t j = 0; j < 3; j++) {
rules[i].main[j] = 0;
rules[i].alt[j] = 0;
}
}
int32_t nrules = 0;
while (fgets(linebuf, BUFSIZ, f) != NULL && *linebuf != '\n') {
char* s = linebuf;
// parse rule index
int32_t rule_idx = 0;
while (isdigit(*s)) {
rule_idx = (rule_idx * 10) + (*s - '0');
s++;
}
rule_t* r = &rules[rule_idx];
r->index = rule_idx;
if (rule_idx >= nrules) {
nrules = rule_idx + 1;
}
s += 2; // ": "
// parse 1st set options
int32_t i;
r->is_char = false;
for (i = 0; *s != '\n' && *s != '|' && *s != '\0';) {
if (*s == '"' && isalpha(*(s + 1))) {
r->is_char = true;
r->ch = *(s + 1);
s += 2;
break;
} else {
r->main[i] = 0;
while (isdigit(*s)) {
r->main[i] = (r->main[i] * 10) + (*s - '0');
s++;
}
i++;
if (*s == ' ')
s++;
}
}
r->nmain = i;
while (*s == ' ')
s++;
if (*s == '|') {
s += 2; // "| "
for (i = 0; *s != '\n' && *s != '\0';) {
r->alt[i] = 0;
while (isdigit(*s)) {
r->alt[i] = (r->alt[i] * 10) + (*s - '0');
s++;
}
i++;
if (*s == ' ')
s++; // ' '
}
r->nalt = i;
}
}
// parse messages
rule_t* zero = &rules[0];
// assert(message_matches_rule(zero, rules, "bbabbbbaabaabba", 0) > 0);
// assert(message_matches_rule(zero, rules, "ababaaaaaabaaab", 0) > 0);
// assert(message_matches_rule(zero, rules, "ababaaaaabbbaba", 0) > 0);
// assert(message_matches_rule(zero, rules, "babbbbaabbbbbabbbbbbaabaaabaaa",
// 0) > 0 && 4); assert(message_matches_rule(zero, rules,
// "bbbbbbbaaaabbbbaaabbabaaa", 0) > 0); assert(message_matches_rule(zero,
// rules, "bbbababbbbaaaaaaaabbababaaababaabab", 0) > 0 && 5);
// assert(message_matches_rule(zero, rules, "baabbaaaabbaaaababbaababb", 0) >
// 0); assert(message_matches_rule(zero, rules,
// "abbbbabbbbaaaababbbbbbaaaababb", 0) > 0);
// assert(message_matches_rule(zero, rules, "aaaaabbaabaaaaababaa", 0) > 0);
// assert(message_matches_rule(zero, rules,
// "aaaabbaabbaaaaaaabbbabbbaaabbaabaaa", 0) > 0);
// assert(message_matches_rule(zero, rules,
// "aabbbbbaabbbaaaaaabbbbbababaaaaabbaaabba", 0) > 0);
int32_t count = 0;
while (fgets(linebuf, BUFSIZ, f) != NULL) {
// check if message matches rule 0
if (message_matches_rule(zero, rules, linebuf, 0) > 0) {
count++;
}
}
printf("%d\n", count);
assert(count == 350);
fclose(f);
return 0;
}