aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarvin Borner2021-12-06 12:20:47 +0100
committerMarvin Borner2021-12-06 12:20:47 +0100
commitf7c5fc36e16664e91343085f5a1c7c87cb76c0ba (patch)
tree7bfe50039d926a268eaeada05d05a38f23016509
parent7fc45a3ac60662a87c91393d311e4e4246fa7f5c (diff)
Efficiency
-rw-r--r--2021/06/Makefile19
-rw-r--r--2021/06/input1
-rw-r--r--2021/06/solve.c81
3 files changed, 101 insertions, 0 deletions
diff --git a/2021/06/Makefile b/2021/06/Makefile
new file mode 100644
index 0000000..4a881b7
--- /dev/null
+++ b/2021/06/Makefile
@@ -0,0 +1,19 @@
+DEBUG = -Wno-error -Og -g -s -fsanitize=undefined -fsanitize=address -fstack-protector-all
+CFLAGS = -Wall -Wextra -Werror -Wshadow -Wpointer-arith -Wwrite-strings -Wredundant-decls -Wnested-externs -Wformat=2 -Wmissing-declarations -Wstrict-prototypes -Wmissing-prototypes -Wcast-qual -Wswitch-default -Wswitch-enum -Wunreachable-code -Wundef -Wold-style-definition -Wvla -pedantic-errors -Ofast
+
+# Not the best makefile but idc
+
+debug:
+ @gcc $(CFLAGS) $(DEBUG) solve.c -o solve.o -L/usr/lib -lcrypto
+
+build:
+ @gcc $(CFLAGS) solve.c -o solve.o -L/usr/lib -lcrypto
+
+clean:
+ @rm -f *.o
+
+run: debug
+ @./solve.o
+
+time: build
+ @./solve.o
diff --git a/2021/06/input b/2021/06/input
new file mode 100644
index 0000000..936e7a7
--- /dev/null
+++ b/2021/06/input
@@ -0,0 +1 @@
+1,2,4,5,5,5,2,1,3,1,4,3,2,1,5,5,1,2,3,4,4,1,2,3,2,1,4,4,1,5,5,1,3,4,4,4,1,2,2,5,1,5,5,3,2,3,1,1,3,5,1,1,2,4,2,3,1,1,2,1,3,1,2,1,1,2,1,2,2,1,1,1,1,5,4,5,2,1,3,2,4,1,1,3,4,1,4,1,5,1,4,1,5,3,2,3,2,2,4,4,3,3,4,3,4,4,3,4,5,1,2,5,2,1,5,5,1,3,4,2,2,4,2,2,1,3,2,5,5,1,3,3,4,3,5,3,5,5,4,5,1,1,4,1,4,5,1,1,1,4,1,1,4,2,1,4,1,3,4,4,3,1,2,2,4,3,3,2,2,2,3,5,5,2,3,1,5,1,1,1,1,3,1,4,1,4,1,2,5,3,2,4,4,1,3,1,1,1,3,4,4,1,1,2,1,4,3,4,2,2,3,2,4,3,1,5,1,3,1,4,5,5,3,5,1,3,5,5,4,2,3,2,4,1,3,2,2,2,1,3,4,2,5,2,5,3,5,5,1,1,1,2,2,3,1,4,4,4,5,4,5,5,1,4,5,5,4,1,1,5,3,3,1,4,1,3,1,1,4,1,5,2,3,2,3,1,2,2,2,1,1,5,1,4,5,2,4,2,2,3
diff --git a/2021/06/solve.c b/2021/06/solve.c
new file mode 100644
index 0000000..069cb09
--- /dev/null
+++ b/2021/06/solve.c
@@ -0,0 +1,81 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+
+#define COUNT(a) ((int)(sizeof(a) / sizeof 0 [a]))
+
+#define FORLINE \
+ char *line = NULL; \
+ size_t len = 0; \
+ while (getline(&line, &len, fp) != EOF)
+#define FREELINE \
+ if (line) \
+ free(line)
+
+#define FORCH \
+ char ch = 0; \
+ while ((ch = fgetc(fp)) != EOF)
+
+#define WHOLE \
+ fseek(fp, 0, SEEK_END); \
+ long fsize = ftell(fp); \
+ fseek(fp, 0, SEEK_SET); \
+ char *data = malloc(fsize + 1); \
+ fread(data, 1, fsize, fp); \
+ data[fsize] = 0;
+/* data[fsize--] = 0 */
+
+static long iterate(char *fish, long cnt)
+{
+ /* for (int i = 0; i < cnt; i++) */
+ /* printf("%d,", fish[i]); */
+ /* printf("\n"); */
+
+ long ret = cnt;
+ for (long i = 0; i < cnt; i++) {
+ if (fish[i] == 0) {
+ fish[i] = 6;
+ fish[ret++] = 8;
+ } else {
+ fish[i]--;
+ }
+ }
+
+ return ret;
+}
+
+static long solve(FILE *fp)
+{
+ char *fish = malloc(20000000000 * sizeof(char));
+
+ long cnt = 0;
+ char ch;
+ while (fscanf(fp, "%d%c", (int *)&fish[cnt++], &ch) != EOF && (ch == ',' || ch == '\n'))
+ ;
+ cnt -= 1; // newline
+
+ for (int i = 0; i < 256; i++)
+ cnt = iterate(fish, cnt);
+
+ free(fish);
+
+ return cnt;
+}
+
+int main(int argc, char *argv[])
+{
+ (void)argc;
+ (void)argv;
+
+ FILE *fp = fopen("input", "r");
+ if (!fp)
+ exit(EXIT_FAILURE);
+
+ clock_t tic = clock();
+ printf("%lu\n", solve(fp));
+ clock_t toc = clock();
+ printf("TIME: %f seconds\n", (double)(toc - tic) / CLOCKS_PER_SEC);
+
+ fclose(fp);
+ return 0;
+}