def part2(data):
    """Multiply the oxygen generator rating with the CO2 scrubber rating.

    They are obtained by considering, for each bit position,
    the numbers that have the most resp. least frequent bit at that position and eliminating the others.
    If the 0 and 1 are equally frequent at a position,
    take the numbers with 1 resp. 0 at that position.
    The rating is the last number remaining.
    """

    def rating(numbers, oxygen: bool):
        """Return rating for the given gas."""
        pos = 0
        while len(numbers) > 1:
            # count 0s and 1s at position pos
            zeros = sum(number[pos] == '0' for number in numbers)
            ones = len(numbers) - zeros
            if oxygen:          # use most frequent bit or 1 if a tie
                bit = '0' if zeros > ones else '1'
            else:               # use least frequent bit or 0 if a tie
                bit = '0' if zeros <= ones else '1'
            # get the numbers with that bit at that position
            numbers = [number for number in numbers if number[pos] == bit]
            pos += 1
        return int(numbers[0], 2)

    lines = data.splitlines()
    return rating(lines, True) * rating(lines, False)