aboutsummaryrefslogtreecommitdiff
path: root/2020/19/solve.c
diff options
context:
space:
mode:
authorMarvin Borner2020-12-19 19:49:19 +0100
committerMarvin Borner2020-12-19 19:49:19 +0100
commit6b488c1fed7801bbae1d79b9b985d19008fd925b (patch)
tree55f08a67a766aff34f823f5312c25006588fe040 /2020/19/solve.c
parent4331232a5a7f5d35bdbe9040dc73e5c69043ba0b (diff)
Blabla
Diffstat (limited to '2020/19/solve.c')
-rw-r--r--2020/19/solve.c60
1 files changed, 50 insertions, 10 deletions
diff --git a/2020/19/solve.c b/2020/19/solve.c
index cea334e..7a8bfa6 100644
--- a/2020/19/solve.c
+++ b/2020/19/solve.c
@@ -1,4 +1,3 @@
-#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -6,12 +5,22 @@
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;
- if (sscanf(rule, "%d %d | %d %d", &num1, &num2, &num3, &num4) == 4) {
+ 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]))) {
@@ -19,6 +28,14 @@ int verify(char *inp, char *rule)
} 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]))) {
@@ -55,7 +72,6 @@ long part_one(FILE *fp)
int paragraph = 0;
char *line = NULL;
size_t len;
- int i = 0;
while (getline(&line, &len, fp) != -1) {
if (line[0] == '\n') {
paragraph++;
@@ -81,22 +97,46 @@ 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 *fp = fopen("input", "r");
- if (!fp)
+ 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(fp));
- rewind(fp);
- printf("%lu\n", part_two(fp));
+ 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(fp);
+ fclose(fp1);
+ fclose(fp2);
return 0;
}