day 7 cleanup

borrow an idea from elly's solution: we don't need separate copies of
each function in order to treat jokers differently, we can just replace
the Js with a different letter
main
magical 2023-12-07 07:34:17 +00:00
parent bea122a661
commit 8081b6f1fa
1 changed files with 17 additions and 35 deletions

View File

@ -8,11 +8,11 @@ proc parse input {
proc solve hands { proc solve hands {
foreach {hand bid} $hands { foreach {hand bid} $hands {
puts "$hand: [type $hand] [typeJ $hand]" puts "$hand: [type $hand] [type [jokerize $hand]]"
} }
set result {} set result {}
foreach cmp {beats beatsJ} { foreach transform {id jokerize} {
set sorted [lsort -index 0 -stride 2 -decreasing -command $cmp $hands] set sorted [lsort -index 0 -stride 2 -decreasing -command beats [$transform $hands]]
#puts $sorted #puts $sorted
set i 0 set i 0
set t 0 set t 0
@ -26,38 +26,23 @@ proc solve hands {
return $result return $result
} }
proc index {c} { return [lsearch {A K Q J T 9 8 7 6 5 4 3 2} $c] } proc id {x} { return $x }
proc indexJ {c} { return [lsearch {A K Q T 9 8 7 6 5 4 3 2 J} $c] } proc jokerize {hands} {
# turn J into jokers (which we represent by an X)
return [string map {J X} $hands]
proc type hand {
array set n {}
foreach c [split $hand ""] {
incr n($c)
}
set groups [lsort -decreasing -stride 2 -index 1 -integer [array get n]]
#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}
default {list 7}
}]
} }
proc typeJ hand { proc index {c} { return [lsearch {A K Q J T 9 8 7 6 5 4 3 2 X} $c] }
# type of hand, but with jokers replaced by the best card
if {$hand eq "JJJJJ"} { proc type hand {
if {$hand eq "XXXXX"} {
# all jokers! # all jokers!
return 1 return 1
} }
array set n {} array set n {}
set j 0 set j 0
foreach c [split $hand ""] { foreach c [split $hand ""] {
if {$c ne "J"} { if {$c ne "X"} {
incr n($c) incr n($c)
} else { } else {
incr j incr j
@ -89,13 +74,10 @@ proc xbeats {hand1 hand2} {
return $x return $x
} }
proc beats {hand1 hand2} { return [generic_beats type index $hand1 $hand2]} proc beats {hand1 hand2} {
proc beatsJ {hand1 hand2} { return [generic_beats typeJ indexJ $hand1 $hand2]}
proc generic_beats {typef indexf hand1 hand2} {
#puts "cmp $hand1 $hand2" #puts "cmp $hand1 $hand2"
set r1 [$typef $hand1] set r1 [type $hand1]
set r2 [$typef $hand2] set r2 [type $hand2]
if {$r1 != $r2} { if {$r1 != $r2} {
return [cmp $r1 $r2] return [cmp $r1 $r2]
} }
@ -103,8 +85,8 @@ proc generic_beats {typef indexf hand1 hand2} {
foreach c1 [split $hand1 ""] c2 [split $hand2 ""] { foreach c1 [split $hand1 ""] c2 [split $hand2 ""] {
#puts "c=$c1 $c2" #puts "c=$c1 $c2"
if {$c1 ne $c2} { if {$c1 ne $c2} {
set i1 [$indexf $c1] set i1 [index $c1]
set i2 [$indexf $c2] set i2 [index $c2]
#puts "i=$i1 $i2" #puts "i=$i1 $i2"
return [cmp $i1 $i2] return [cmp $i1 $i2]
} }