day 7 more cleanup

we don't need to know the card identities when determining hand-type,
just the counts
main
magical 2023-12-07 07:37:07 +00:00
parent 8081b6f1fa
commit ba438b7b05
1 changed files with 12 additions and 11 deletions

View File

@ -40,26 +40,27 @@ proc type hand {
return 1
}
array set n {}
set j 0
set jokers 0
foreach c [split $hand ""] {
if {$c ne "X"} {
incr n($c)
} else {
incr j
incr jokers
}
}
set groups [lsort -decreasing -stride 2 -index 1 -integer [array get n]]
set counts [lmap {k v} [array get n] {list $v}]
set counts [lsort -decreasing -integer $counts]
# more of the same card is always better,
# so add the jokers to the most frequent card
lset groups 1 [expr {$j + [lindex $groups 1]}]
lset counts 0 [expr {$jokers + [lindex $counts 0]}]
#puts $groups
return [switch -glob $groups {
{? 5} {{#} Five of a kind; list 1}
{? 4 *} {{#} Four of a kind; list 2}
{? 3 ? 2} {{#} Full house; list 3}
{? 3 *} {{#} Three of a kind; list 4}
{? 2 ? 2 *} {{#} Two pair; list 5}
{? 2 *} {{#} One pair; list 6}
return [switch -glob $counts {
{5} {{#} Five of a kind; list 1}
{4 *} {{#} Four of a kind; list 2}
{3 2} {{#} Full house; list 3}
{3 *} {{#} Three of a kind; list 4}
{2 2 *} {{#} Two pair; list 5}
{2 *} {{#} One pair; list 6}
default {list 7}
}]
}