[ <combo> [ choose [0,b) ] keep ] dip
'[ _ apply-combination @ ] each ; inline
+: map-combinations ( seq k quot -- )
+ [ <combo> [ choose [0,b) ] keep ] dip
+ '[ _ apply-combination @ ] map ; inline
+
+: reduce-combinations ( seq k identity quot -- result )
+ [ -rot ] dip each-combination ; inline
+
[ t ] [ "7C 5D 4H 3S 2C" "7D 5D 4D 3C 2S" [ <hand> ] bi@ = ] unit-test
[ f ] [ "7C 5D 4H 3S 2C" "7D 5D 4D 3C 2S" [ <hand> ] bi@ eq? ] unit-test
+
+[ 190 ] [ "AS KD JC KH 2D 2S KH" best-hand value>> ] unit-test
TUPLE: hand
{ cards sequence }
- { value integer } ;
+ { value integer initial: 9999 } ;
M: hand <=> [ value>> ] compare ;
M: hand equal?
parse-cards dup hand-value hand boa ;
: best-hand ( str -- hand )
- parse-cards 5 all-combinations
- [ dup hand-value hand boa ] map infimum ;
+ parse-cards 5 hand new
+ [ dup hand-value hand boa min ] reduce-combinations ;
: >cards ( hand -- str )
cards>> [ card>string ] map " " join ;