2023-12-07 06:04:18 +00:00
|
|
|
#!/usr/bin/env tclsh
|
|
|
|
#source ../prelude.tcl
|
2023-12-07 07:01:53 +00:00
|
|
|
proc {#} args {}
|
2023-12-07 06:04:18 +00:00
|
|
|
|
|
|
|
proc parse input {
|
|
|
|
return [join [split [read $input] "\n"]]
|
|
|
|
}
|
|
|
|
|
|
|
|
proc solve hands {
|
|
|
|
foreach {hand bid} $hands {
|
2023-12-07 07:34:17 +00:00
|
|
|
puts "$hand: [type $hand] [type [jokerize $hand]]"
|
2023-12-07 06:04:18 +00:00
|
|
|
}
|
2023-12-07 07:01:53 +00:00
|
|
|
set result {}
|
2023-12-07 07:34:17 +00:00
|
|
|
foreach transform {id jokerize} {
|
|
|
|
set sorted [lsort -index 0 -stride 2 -decreasing -command beats [$transform $hands]]
|
2023-12-07 07:01:53 +00:00
|
|
|
#puts $sorted
|
|
|
|
set i 0
|
|
|
|
set t 0
|
|
|
|
foreach {hand bid} $sorted {
|
|
|
|
incr i
|
|
|
|
incr t [expr {$i*$bid}]
|
|
|
|
}
|
|
|
|
#puts [lmap {a b} $sorted {list $b}]
|
|
|
|
lappend result $t
|
2023-12-07 06:04:18 +00:00
|
|
|
}
|
2023-12-07 07:01:53 +00:00
|
|
|
return $result
|
2023-12-07 06:04:18 +00:00
|
|
|
}
|
|
|
|
|
2023-12-07 07:34:17 +00:00
|
|
|
proc id {x} { return $x }
|
|
|
|
proc jokerize {hands} {
|
|
|
|
# turn J into jokers (which we represent by an X)
|
|
|
|
return [string map {J X} $hands]
|
|
|
|
}
|
2023-12-07 06:04:18 +00:00
|
|
|
|
2023-12-07 07:34:17 +00:00
|
|
|
proc index {c} { return [lsearch {A K Q J T 9 8 7 6 5 4 3 2 X} $c] }
|
2023-12-07 06:04:18 +00:00
|
|
|
|
|
|
|
proc type hand {
|
2023-12-07 07:34:17 +00:00
|
|
|
if {$hand eq "XXXXX"} {
|
2023-12-07 07:01:53 +00:00
|
|
|
# all jokers!
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
array set n {}
|
2023-12-07 07:37:07 +00:00
|
|
|
set jokers 0
|
2023-12-07 07:01:53 +00:00
|
|
|
foreach c [split $hand ""] {
|
2023-12-07 07:34:17 +00:00
|
|
|
if {$c ne "X"} {
|
2023-12-07 07:01:53 +00:00
|
|
|
incr n($c)
|
|
|
|
} else {
|
2023-12-07 07:37:07 +00:00
|
|
|
incr jokers
|
2023-12-07 07:01:53 +00:00
|
|
|
}
|
|
|
|
}
|
2023-12-07 07:37:07 +00:00
|
|
|
set counts [lmap {k v} [array get n] {list $v}]
|
|
|
|
set counts [lsort -decreasing -integer $counts]
|
2023-12-07 07:01:53 +00:00
|
|
|
# more of the same card is always better,
|
|
|
|
# so add the jokers to the most frequent card
|
2023-12-07 07:37:07 +00:00
|
|
|
lset counts 0 [expr {$jokers + [lindex $counts 0]}]
|
2023-12-07 07:01:53 +00:00
|
|
|
#puts $groups
|
2023-12-07 07:37:07 +00:00
|
|
|
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}
|
2023-12-07 06:04:18 +00:00
|
|
|
default {list 7}
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
|
|
|
|
proc cmp {a b} { return [expr {($b < $a) - ($a < $b)}] }
|
|
|
|
|
|
|
|
proc xbeats {hand1 hand2} {
|
|
|
|
set x [beats $hand1 $hand2]
|
|
|
|
if {$x < 0} {puts "$hand1 beats $hand2"} \
|
|
|
|
elseif {$x > 0} {puts "$hand2 beats $hand1"} \
|
|
|
|
else {puts "$hand1 ties $hand2"}
|
|
|
|
return $x
|
|
|
|
}
|
|
|
|
|
2023-12-07 07:34:17 +00:00
|
|
|
proc beats {hand1 hand2} {
|
2023-12-07 06:04:18 +00:00
|
|
|
#puts "cmp $hand1 $hand2"
|
2023-12-07 07:34:17 +00:00
|
|
|
set r1 [type $hand1]
|
|
|
|
set r2 [type $hand2]
|
2023-12-07 06:04:18 +00:00
|
|
|
if {$r1 != $r2} {
|
|
|
|
return [cmp $r1 $r2]
|
|
|
|
}
|
|
|
|
#puts "r= $r1 $r2"
|
|
|
|
foreach c1 [split $hand1 ""] c2 [split $hand2 ""] {
|
|
|
|
#puts "c=$c1 $c2"
|
|
|
|
if {$c1 ne $c2} {
|
2023-12-07 07:34:17 +00:00
|
|
|
set i1 [index $c1]
|
|
|
|
set i2 [index $c2]
|
2023-12-07 06:04:18 +00:00
|
|
|
#puts "i=$i1 $i2"
|
|
|
|
return [cmp $i1 $i2]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
error "$hand1 ties $hand2"
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
puts [solve [parse stdin]]
|