advent2023

Advent of Code 2023 solutions
git clone git://bsandro.tech/advent2023
Log | Files | Refs | LICENSE

commit d00df266d39a97e3e9c4042f7b5016ad5000de8c
parent 30a28fb0acb99550ad4370b820ab6a78cbff727b
Author: bsandro <email@bsandro.tech>
Date:   Sat, 13 Jan 2024 23:27:51 +0200

day 07 p2

Diffstat:
Mday07/puzzle.c | 66++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
1 file changed, 62 insertions(+), 4 deletions(-)

diff --git a/day07/puzzle.c b/day07/puzzle.c @@ -34,7 +34,7 @@ typedef struct Hand { int32_t csum; } Hand; -int get_card(char c) { +static int get_card(char c) { if (c>='2'&&c<='9') return c-'0'; if (c=='T') return 10; if (c=='J') return 11; @@ -44,6 +44,16 @@ int get_card(char c) { return -1; } +static int get_card2(char c) { + if (c>='2'&&c<='9') return c-'0'; + if (c=='T') return 10; + if (c=='J') return 1; + if (c=='Q') return 12; + if (c=='K') return 13; + if (c=='A') return 14; + return -1; +} + void make_type(Hand *h) { int sum = 10000*h->counts[0] + 1000*h->counts[1] + @@ -53,10 +63,27 @@ void make_type(Hand *h) { h->type = (HandType)sum; } -void make_counts(Hand *h) { +void make_counts(Hand *h, bool use_joker) { + memset(h->counts, 0, sizeof(int)*COUNTS_LEN); for (int i=0; i<CARDS_LEN; ++i) { h->counts[h->cards[i]]++; } + + int jk = get_card2('J'); + if (use_joker && h->counts[jk]>0) { + printf("(jk:%d)", h->counts[jk]); + int *curmax = &h->counts[COUNTS_LEN-1]; + for (int i=COUNTS_LEN-1; i>=0; --i) { + if (i!=jk) { + if (h->counts[i] > *curmax) { + curmax = &h->counts[i]; + } + } + } + //printf("(curmax:%d)", *curmax); + *curmax += h->counts[jk]; + h->counts[jk] = 0; + } } void make_csum(Hand *h) { @@ -67,6 +94,14 @@ void make_csum(Hand *h) { get_card(h->cards_orig[4]); } +void make_csum2(Hand *h) { + h->csum = 100000000*get_card2(h->cards_orig[0]) + + 1000000*get_card2(h->cards_orig[1]) + + 10000*get_card2(h->cards_orig[2]) + + 100*get_card2(h->cards_orig[3]) + + get_card2(h->cards_orig[4]); +} + static int cmp_cards(const void *c1, const void *c2) { return *(int *)c1 - *(int *)c2; } @@ -108,14 +143,17 @@ void puzzle(const char *filename, long long *result1, long long *result2) { h->cards[i] = get_card(h->cards_orig[i]); } qsort(h->cards, CARDS_LEN, sizeof(int), cmp_cards); - make_counts(h); + make_counts(h, false); make_csum(h); qsort(h->counts, COUNTS_LEN, sizeof(int), cmp_counts); make_type(h); + // + ++line_num; bzero(buf, STR_LEN); } + printf("--------- part 1 ----------\n"); qsort(hands, hands_len, sizeof(Hand), cmp_hands); for (int i=0; i<hands_len; ++i) { printf("hand %2d %s, type %d, bid %3d; ", i, hands[i].cards_orig, hands[i].type, hands[i].bid); @@ -129,10 +167,30 @@ void puzzle(const char *filename, long long *result1, long long *result2) { } } printf("\n"); - *result1 += (i+1)*hands[i].bid; } + // 246452271 too high + printf("----------- part 2 ------------\n"); + for (int i=0; i<hands_len; ++i) { + Hand *h = &hands[i]; + printf("hand %s: was type %d, now ", h->cards_orig, h->type); + memset(h->cards, 0, sizeof(int)*CARDS_LEN); + for (int j=0; j<CARDS_LEN; ++j) { + h->cards[j] = get_card2(h->cards_orig[j]); + } + make_counts(h, true); + make_csum2(h); + qsort(h->counts, COUNTS_LEN, sizeof(int), cmp_counts); + make_type(h); + printf("%d\n", h->type); + } + qsort(hands, hands_len, sizeof(Hand), cmp_hands); + for (int i=0; i<hands_len; ++i) { + printf("hand %2d %s type %d\n", i, hands[i].cards_orig, hands[i].type); + *result2 += (i+1)*hands[i].bid; + } + // mutiny! ignoring feof/ferror. fclose(infile); }