diff --git a/day07/sol.tcl b/day07/sol.tcl index 6c34f65..3b27e94 100644 --- a/day07/sol.tcl +++ b/day07/sol.tcl @@ -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} }] }