ShareMyScreen/AdventOfCode/2023/07/CamelCards/rdm
1sample=: {{)n
232T3K 765
3T55J5 684
4KK677 28
5KTJJT 220
6QQQJA 483
7}}
8
9day7=: ([fwrite&'2023-day7.txt') fread '~/Downloads/input.txt'
10
11parse=: cut@>@cutLF
Here we had "cards" and a vaguely poker-like scoring system. But not really poker. Basically, ranking of hands looked like this:
13val=: '23456789TJQKA'&i.
14rank=: {{ (\:~ #/.~ y),val y }}"1
In other words, we count up number of identical cards and sort first by whatever the largest of those were. And to break ties we'd use any remaining count of identical cards. And, to break those cards, we'd use the card ranking of the cards in order of their appearance.
Part 1, wanted us to rank cards and multiply the rank index by the hand's number.
16score=: {{ +/(1+i.#y)*(0 ".1{::"1 y) /: rank 0{::"1 y}}
The hard part here is just understanding the rules. But I'm not going to do any better explaining them than reading the aoc puzzle page (or, than the code here).
score parse sample
6440
Part 2 made 'J' cards wild, so for the purpose of ranking we had to pick an alternate form of the hand with the J's replaced by whichever "card" gave us the best score.
'hands bids'=:|:parse sample
best=: {{{.(\: rank) y rplc"1'J',.y}}"1
best hands
32T3K
T5555
KK677
KTTTT
QQQQA
For the tie breaker, we used a slightly different scheme where 'J' was the lowest card.
18val2=: 'J23456789TQKA'&i.
19best=: {{{.(\: rank) y rplc"1'J',.y}}"1
20rank2=: {{ (\:~ #/.~ y),val2 y }}"1
21score2=: {{ +/(1+i.#y)*(0 ".1{::"1 y) /: rank2 best 0 {::"1 y}}
A bit messy, but it got the job done.
score2 parse sample
5905
score2 parse day7
(If you think this code could have been written more clearly, you would be right. And, please feel free to do so.)