beats Function

function beats(hand1, hand2, with_jokers)

return true if hand1 beats hand2 (has a higher score)

Arguments

Type IntentOptional Attributes Name
class(hand), intent(inout) :: hand1
class(hand), intent(inout) :: hand2
logical, intent(in) :: with_jokers

if considering jokers

Return Value logical


Calls

proc~~beats~~CallsGraph proc~beats problem_7::beats proc~hand_type problem_7::hand_type proc~beats->proc~hand_type proc~index_in_cards problem_7::index_in_cards proc~beats->proc~index_in_cards interface~unique~2 aoc_utilities::unique proc~hand_type->interface~unique~2 proc~unique32 aoc_utilities::unique32 interface~unique~2->proc~unique32 proc~unique64 aoc_utilities::unique64 interface~unique~2->proc~unique64 interface~sort aoc_utilities::sort proc~unique32->interface~sort proc~unique64->interface~sort proc~sort_ascending aoc_utilities::sort_ascending interface~sort->proc~sort_ascending proc~sort_ascending_64 aoc_utilities::sort_ascending_64 interface~sort->proc~sort_ascending_64 interface~swap~2 aoc_utilities::swap proc~sort_ascending->interface~swap~2 proc~swap64 aoc_utilities::swap64 proc~sort_ascending_64->proc~swap64 interface~swap~2->proc~swap64 proc~swap32 aoc_utilities::swap32 interface~swap~2->proc~swap32 proc~swap_str aoc_utilities::swap_str interface~swap~2->proc~swap_str

Called by

proc~~beats~~CalledByGraph proc~beats problem_7::beats program~problem_7 problem_7 program~problem_7->proc~beats

Source Code

    logical function beats(hand1, hand2, with_jokers)
        !! return true if hand1 beats hand2 (has a higher score)
        class(hand),intent(inout) :: hand1, hand2
        logical,intent(in) :: with_jokers !! if considering jokers
        integer :: i

        associate(h1 => hand1%cards, h2 => hand2%cards)
            ! recompute type if it hasn't already been computed
            if (hand1%type==0) hand1%type = hand_type(hand1, with_jokers)
            if (hand2%type==0) hand2%type = hand_type(hand2, with_jokers)
            if (hand1%type==hand2%type) then
                ! lower index is stronger
                do i = 1, 5
                    if (h1(i)/=h2(i)) then
                        beats = index_in_cards(h1(i),with_jokers) < &
                                index_in_cards(h2(i),with_jokers) ! lower is stronger
                        return
                    end if
                end do
            else
                ! one hand beat the other
                beats = hand1%type < hand2%type  ! lower score is better (1-7)
            end if
        end associate
    end function beats