diff options
Diffstat (limited to '2023/07/7a.c')
| -rw-r--r-- | 2023/07/7a.c | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/2023/07/7a.c b/2023/07/7a.c new file mode 100644 index 0000000..ffc9b5c --- /dev/null +++ b/2023/07/7a.c | |||
| @@ -0,0 +1,58 @@ | |||
| 1 | #include <inttypes.h> | ||
| 2 | #include <stdio.h> | ||
| 3 | #include <stdlib.h> | ||
| 4 | #include <string.h> | ||
| 5 | |||
| 6 | #define N 100000 | ||
| 7 | |||
| 8 | int value[255] = { | ||
| 9 | ['2'] = 2, ['3'] = 3, ['4'] = 4, ['5'] = 5, | ||
| 10 | ['6'] = 6, ['7'] = 7, ['8'] = 8, ['9'] = 9, | ||
| 11 | ['T'] = 10, ['J'] = 11, ['Q'] = 12, ['K'] = 13, ['A'] = 14 | ||
| 12 | }; | ||
| 13 | |||
| 14 | typedef struct { | ||
| 15 | char c[5]; | ||
| 16 | int64_t b; | ||
| 17 | } hand_t; | ||
| 18 | |||
| 19 | int strength(hand_t h) { | ||
| 20 | int i, c2, c1, s, x[15]; | ||
| 21 | |||
| 22 | memset(x, 0, 15 * sizeof(int)); | ||
| 23 | for (i = 0, s = 0; i < 5; i++) { | ||
| 24 | s = s * 15 + value[h.c[i]]; | ||
| 25 | x[value[h.c[i]]]++; | ||
| 26 | } | ||
| 27 | for (i = 2, c1 = 0, c2 = 0; i < 15; i++) { | ||
| 28 | if (c1 < x[i]) { | ||
| 29 | c2 = c1; | ||
| 30 | c1 = x[i]; | ||
| 31 | } else if (c2 < x[i]) c2 = x[i]; | ||
| 32 | } | ||
| 33 | |||
| 34 | return (c1*5 + c2) * 759375 + s; | ||
| 35 | } | ||
| 36 | |||
| 37 | int compare(const void *h1, const void *h2) { | ||
| 38 | return strength(*(hand_t *)h1) - strength(*(hand_t *)h2); | ||
| 39 | } | ||
| 40 | |||
| 41 | int main() { | ||
| 42 | char line[N]; | ||
| 43 | hand_t hand[N]; | ||
| 44 | int64_t i, n, p; | ||
| 45 | |||
| 46 | for (n = 0; fgets(line, N, stdin) != NULL; n++) { | ||
| 47 | memcpy(hand[n].c, line, 5); | ||
| 48 | hand[n].b = atoll(line+6); | ||
| 49 | } | ||
| 50 | |||
| 51 | qsort(hand, n, sizeof(hand_t), &compare); | ||
| 52 | |||
| 53 | for (i = 0, p = 0; i < n; i++) | ||
| 54 | p += hand[i].b * (i+1); | ||
| 55 | |||
| 56 | printf("%" PRId64 "\n", p); | ||
| 57 | return 0; | ||
| 58 | } | ||
