22 my @queue = [ 0, $p[0], $p[1], 0, 0 ];
25 my %states = ( "$p[0],$p[1],0,0" => 1 );
29 while (my $q = pop_heap @queue) {
31 my $count = $states{$q->[1],$q->[2],$q->[3],$q->[4]};
32 # say "pop: ", join(',', @$q);
34 my ($sum, $p1, $p2, $s1, $s2) = @$q;
36 $p1 -= 10 while $p1 > 10;
38 my $c1 = $count * $state_count{$d1};
45 my ($np2, $ns2, $c2) = ($p2, $s2, $c1);
47 $np2 -= 10 while $np2 > 10;
49 $c2 *= $state_count{$d2};
54 my $key = "$p1,$np2,$s1,$ns2";
60 push_heap @queue, [ $s1+$ns2, $p1, $np2, $s1, $ns2 ];
64 # say "queue: ", scalar @queue, " states ", scalar keys %states;
67 say $wins[0], ' vs ', $wins[1], " winner is ", $wins[0] > $wins[1] ? 'first' : 'second';