uppga.c (1426B)
1 #include "common.h" 2 3 int cmp(const void *a, const void *b); 4 5 int cmp(const void *a, const void *b) { 6 size_t i; 7 8 hand_t ha = *(const hand_t *)a; 9 hand_t hb = *(const hand_t *)b; 10 11 uint64_t ha_val = hand_value(ha); 12 uint64_t hb_val = hand_value(hb); 13 14 if (ha_val != hb_val) { 15 if (ha_val < hb_val) 16 return -1; 17 else 18 return 1; 19 } else { 20 for (i = 0; i < 5; i++) { 21 if (ha.cards[i] != hb.cards[i]) { 22 if (ha.cards[i] < hb.cards[i]) 23 return -1; 24 else 25 return 1; 26 } 27 } 28 } 29 30 return 0; 31 } 32 33 static inline uint8_t char_to_card(const char c) { 34 if ('2' <= c && c <= '9') 35 return (uint8_t)(c - '0'); 36 switch (c) { 37 case 'T': 38 return 10; 39 case 'J': 40 return 11; 41 case 'Q': 42 return 12; 43 case 'K': 44 return 13; 45 case 'A': 46 return 14; 47 default: 48 exit(1); 49 } 50 } 51 52 static inline void read_hands(hand_t *h, char **lines, const size_t nlines) { 53 size_t i, j; 54 55 for (i = 0; i < nlines; i++) { 56 for (j = 0; j < 5; j++) 57 h[i].cards[j] = char_to_card(lines[i][j]); 58 sread_next_u64(&(h[i].bid), &lines[i][6]); 59 } 60 } 61 62 int main(int argc, char **argv) { 63 char **lines; 64 size_t nlines = readlines(&lines, "input"); 65 66 hand_t h[nlines]; 67 read_hands(h, lines, nlines); 68 69 qsort(h, nlines, sizeof(h[0]), cmp); 70 71 uint64_t m; 72 uint64_t sum = 0; 73 for (m = 0; m < nlines; m++) { 74 sum += (m + 1) * h[m].bid; 75 } 76 77 printf("%llu\n", sum); 78 }