aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarvin Borner2020-12-23 21:41:20 +0100
committerMarvin Borner2020-12-23 22:12:06 +0100
commita98af220d1b4b31a540caa9d5b88dd6cd5303717 (patch)
treeabd67f9ba30c8db80c3b534ad970367a76f8ce53
parentec00b6eb28c484ea07212fc7582176a5b2e187e1 (diff)
Added quick and dirty C solution for b
-rw-r--r--2020/23/Makefile3
-rw-r--r--2020/23/solve.c52
2 files changed, 55 insertions, 0 deletions
diff --git a/2020/23/Makefile b/2020/23/Makefile
index dad052e..197dba2 100644
--- a/2020/23/Makefile
+++ b/2020/23/Makefile
@@ -1,2 +1,5 @@
+c:
+ @gcc -Ofast solve.c && ./a.out
+
run:
@node solve.js
diff --git a/2020/23/solve.c b/2020/23/solve.c
new file mode 100644
index 0000000..ab7e878
--- /dev/null
+++ b/2020/23/solve.c
@@ -0,0 +1,52 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define SZ 9
+
+int *play(int *I, int C, int M)
+{
+ int *c = malloc((C + 1) * sizeof(*c));
+ if (!c)
+ exit(1);
+
+ for (int i = 0; i < C; i++)
+ c[i] = i + 1;
+
+ for (int i = 0; i < C + 1; i++)
+ c[I[i]] = I[(i + 1) % SZ];
+
+ c[0] = I[0];
+ if (C > SZ) {
+ c[I[SZ - 1]] = SZ + 1;
+ c[C] = I[0];
+ }
+
+ for (int m = 1; m <= M; m++) {
+ int p1 = c[c[0]];
+ int p2 = c[c[c[0]]];
+ int p3 = c[c[c[c[0]]]];
+ int d = c[0] - 1 ? c[0] - 1 : C;
+ while (p1 == d || p2 == d || p3 == d)
+ d = d - 1 ? d - 1 : C;
+ c[c[0]] = c[p3];
+ c[p3] = c[d];
+ c[d] = p1;
+ c[0] = c[c[0]];
+ }
+
+ return c;
+}
+
+int main(int argc, char *argv[])
+{
+ int data[] = { 4, 6, 3, 5, 2, 8, 1, 7, 9 };
+ int *d = malloc((1e6 + 1) * sizeof(*d));
+ memcpy(d, data, 9 * 4);
+ int *c = play(d, 1e6, 1e7);
+ long res = ((long)c[1]) * ((long)c[c[1]]);
+ printf("%lu\n", res);
+ free(d);
+ free(c);
+ return 0;
+}