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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
static char rules[256][256] = { 0 };
// Who cares about code repetition? :P
static int ind = 0;
int verify(char *inp, char *rule)
{
char ch;
int num1, num2, num3, num4, num5;
if (sscanf(rule, "%d %d | %d %d %d", &num1, &num2, &num3, &num4, &num5) == 5) {
int i = ind;
if ((verify(inp, rules[num1]) && verify(inp, rules[num2])) ||
((ind = i), verify(inp, rules[num3]) && verify(inp, rules[num4]) &&
verify(inp, rules[num5]))) {
return ind;
} else {
return 0;
}
} else if (sscanf(rule, "%d %d | %d %d", &num1, &num2, &num3, &num4) == 4) {
int i = ind;
if ((verify(inp, rules[num1]) && verify(inp, rules[num2])) ||
((ind = i), verify(inp, rules[num3]) && verify(inp, rules[num4]))) {
return ind;
} else {
return 0;
}
} else if (sscanf(rule, "%d | %d %d", &num1, &num2, &num3) == 3) {
int i = ind;
if (verify(inp, rules[num1]) ||
((ind = i), verify(inp, rules[num2]) && verify(inp, rules[num3]))) {
return ind;
} else {
return 0;
}
} else if (sscanf(rule, "%d | %d", &num1, &num2) == 2) {
int i = ind;
if (verify(inp, rules[num1]) || ((ind = i), verify(inp, rules[num2]))) {
return ind;
} else {
return 0;
}
} else if (sscanf(rule, "%d %d", &num1, &num2) == 2) {
if (verify(inp, rules[num1]) && verify(inp, rules[num2])) {
return ind;
} else {
return 0;
}
} else if (sscanf(rule, "%d", &num1) == 1) {
if (verify(inp, rules[num1])) {
return ind;
} else {
return 0;
}
} else if (sscanf(rule, "\"%c\"", &ch) == 1) {
return ch == inp[ind] ? ++ind : 0;
} else {
printf("PANIC: %s, %s\n", inp, rule);
exit(0);
}
return 0;
}
long part_one(FILE *fp)
{
long res = 0;
int paragraph = 0;
char *line = NULL;
size_t len;
while (getline(&line, &len, fp) != -1) {
if (line[0] == '\n') {
paragraph++;
continue;
}
if (paragraph == 0) {
char rule[64] = { 0 };
int ind = 0;
sscanf(line, "%d: %64[0-9a-z \"|]", &ind, rule);
strcpy(rules[ind], rule);
} else if (paragraph == 1) {
ind = 0;
if (verify(line, rules[0]) == strlen(line) - 1)
res++;
}
}
return res;
}
long part_two(FILE *fp)
{
long res = 0;
int paragraph = 0;
char *line = NULL;
size_t len;
while (getline(&line, &len, fp) != -1) {
if (line[0] == '\n') {
paragraph++;
continue;
}
if (paragraph == 0) {
char rule[64] = { 0 };
int ind = 0;
sscanf(line, "%d: %64[0-9a-z \"|]", &ind, rule);
strcpy(rules[ind], rule);
} else if (paragraph == 1) {
ind = 0;
if (verify(line, rules[0]) == strlen(line) - 1)
res++;
}
}
return res;
}
int main(int argc, char *argv[])
{
FILE *fp1 = fopen("input1", "r");
if (!fp1)
exit(EXIT_FAILURE);
FILE *fp2 = fopen("input2", "r");
if (!fp2)
exit(EXIT_FAILURE);
clock_t tic = clock();
printf("%lu\n", part_one(fp1));
printf("%lu\n", part_two(fp2));
clock_t toc = clock();
printf("TIME: %f seconds\n", (double)(toc - tic) / CLOCKS_PER_SEC);
fclose(fp1);
fclose(fp2);
return 0;
}
|