aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarvin Borner2020-12-22 15:18:04 +0100
committerMarvin Borner2020-12-22 15:18:04 +0100
commit8ee4d039237f50d818b487a65c08c1ef3905f909 (patch)
tree8297226ed712c3861dbda18134e5148fb865a861
parentc74e74acdc796bdcc22251ff6d837966c80e8154 (diff)
idk, way too much mallocing... idc
-rw-r--r--2020/22/solve.c39
1 files changed, 24 insertions, 15 deletions
diff --git a/2020/22/solve.c b/2020/22/solve.c
index a34a718..fa2ebd1 100644
--- a/2020/22/solve.c
+++ b/2020/22/solve.c
@@ -3,13 +3,14 @@
#include <string.h>
#include <time.h>
-#define BUFFER 512
+#define BUFFER 10000
+#define DUP_SIZE 10000
long part_one(FILE *fp)
{
long res = 0;
- int player_one[BUFFER] = { 0 }, player_two[BUFFER] = { 0 };
+ int player_one[512] = { 0 }, player_two[512] = { 0 };
int one_low = 0, one_high = 0, two_low = 0, two_high = 0;
char *line = NULL;
@@ -32,6 +33,7 @@ long part_one(FILE *fp)
player_two[index++] = num;
}
}
+ free(line);
one_high = two_high = index;
@@ -71,30 +73,33 @@ int recursive(int *player_one, int *player_two, int *one_low, int *one_high, int
memcpy(&two_copy[*two_low], &player_two[*two_low], (*two_high - *two_low) * sizeof(int));
int dup_ind = 0;
- int dup_one[64][BUFFER] = { 0 };
- int dup_two[64][BUFFER] = { 0 };
+ int(*dup_one)[DUP_SIZE] = malloc(DUP_SIZE * BUFFER * sizeof(int));
+ int(*dup_two)[DUP_SIZE] = malloc(DUP_SIZE * BUFFER * sizeof(int));
while (1) {
// Check for duplicates.. (hashmaps would be nice)
for (int i = 0; i < dup_ind; i++) {
if (!memcmp(dup_one[i], &one_copy[*one_low],
- (*one_high - *one_low) * sizeof(int)) ||
+ (*one_high - *one_low + 1) * sizeof(int)) ||
!memcmp(dup_two[i], &two_copy[*two_low],
- (*two_high - *two_low) * sizeof(int)))
+ (*two_high - *two_low + 1) * sizeof(int))) {
+ free(dup_one);
+ free(dup_two);
return 0;
+ }
}
memcpy(dup_one[dup_ind], &one_copy[*one_low], (*one_high - *one_low) * sizeof(int));
memcpy(dup_two[dup_ind++], &two_copy[*two_low],
(*two_high - *two_low) * sizeof(int));
- if (dup_ind >= 64)
+ if (dup_ind >= DUP_SIZE)
exit(1);
- for (int *p = &one_copy[*one_low]; *p; p++)
- printf("%d ", *p);
- printf(" (L1: %d, H1: %d)\n", *one_low, *one_high);
- for (int *p = &two_copy[*two_low]; *p; p++)
- printf("%d ", *p);
- printf(" (L2: %d, H2: %d)\n\n", *two_low, *two_high);
+ /* for (int *p = &one_copy[*one_low]; *p; p++) */
+ /* printf("%d ", *p); */
+ /* printf(" (L1: %d, H1: %d)\n", *one_low, *one_high); */
+ /* for (int *p = &two_copy[*two_low]; *p; p++) */
+ /* printf("%d ", *p); */
+ /* printf(" (L2: %d, H2: %d)\n\n", *two_low, *two_high); */
int c1 = one_copy[(*one_low)++];
int c2 = two_copy[(*two_low)++];
@@ -104,14 +109,14 @@ int recursive(int *player_one, int *player_two, int *one_low, int *one_high, int
int rec_won = -1;
if (*one_high + 1 - *one_low > c1 && *two_high + 1 - *two_low > c2) {
- printf("AAH RECURSION: %d %d\n", c1, c2);
+ /* printf("AAH RECURSION: %d %d\n", c1, c2); */
int rec_one_low = *one_low;
int rec_one_high = *one_low + c1;
int rec_two_low = *two_low;
int rec_two_high = *two_low + c2;
rec_won = recursive(one_copy, two_copy, &rec_one_low, &rec_one_high,
&rec_two_low, &rec_two_high, iter + 1);
- printf("REC: %d\n", rec_won);
+ /* printf("REC: %d\n", rec_won); */
}
if (rec_won == -1) {
@@ -138,6 +143,9 @@ int recursive(int *player_one, int *player_two, int *one_low, int *one_high, int
memcpy(player_two, two_copy, BUFFER);
}
+ free(dup_one);
+ free(dup_two);
+
return one_low != one_high + 1;
}
@@ -168,6 +176,7 @@ long part_two(FILE *fp)
player_two[index++] = num;
}
}
+ free(line);
one_high = two_high = index;