From 554ad47f09974f0131df285fda8ea50a1fdad8f5 Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Mon, 11 Dec 2023 15:14:47 +0100 Subject: Initial commit, some solutions --- 2023/07/7b.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 2023/07/7b.c (limited to '2023/07/7b.c') diff --git a/2023/07/7b.c b/2023/07/7b.c new file mode 100644 index 0000000..58716b3 --- /dev/null +++ b/2023/07/7b.c @@ -0,0 +1,58 @@ +#include +#include +#include +#include + +#define N 100000 + +int value[255] = { + ['2'] = 2, ['3'] = 3, ['4'] = 4, ['5'] = 5, + ['6'] = 6, ['7'] = 7, ['8'] = 8, ['9'] = 9, + ['T'] = 10, ['J'] = 1, ['Q'] = 12, ['K'] = 13, ['A'] = 14 +}; + +typedef struct { + char c[5]; + int64_t b; +} hand_t; + +int strength(hand_t h) { + int i, c2, c1, s, x[15]; + + memset(x, 0, 15 * sizeof(int)); + for (i = 0, s = 0; i < 5; i++) { + s = s * 15 + value[h.c[i]]; + x[value[h.c[i]]]++; + } + for (i = 2, c1 = 0, c2 = 0; i < 15; i++) { + if (c1 < x[i]) { + c2 = c1; + c1 = x[i]; + } else if (c2 < x[i]) c2 = x[i]; + } + + return ((c1+x[1])*5 + c2) * 759375 + s; +} + +int compare(const void *h1, const void *h2) { + return strength(*(hand_t *)h1) - strength(*(hand_t *)h2); +} + +int main() { + char line[N]; + hand_t hand[N]; + int64_t i, n, p; + + for (n = 0; fgets(line, N, stdin) != NULL; n++) { + memcpy(hand[n].c, line, 5); + hand[n].b = atoll(line+6); + } + + qsort(hand, n, sizeof(hand_t), &compare); + + for (i = 0, p = 0; i < n; i++) + p += hand[i].b * (i+1); + + printf("%" PRId64 "\n", p); + return 0; +} -- cgit v1.3