From a98af220d1b4b31a540caa9d5b88dd6cd5303717 Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Wed, 23 Dec 2020 21:41:20 +0100 Subject: Added quick and dirty C solution for b --- 2020/23/solve.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 2020/23/solve.c (limited to '2020/23/solve.c') 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 +#include +#include + +#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; +} -- cgit v1.2.3