Bash Fish
fish() {
local -n _a="$1"
local -n _b="$2"
local _size=${#_a[@]}
local _dead=0
local -a _stack=()
local _i
for ((_i = 0; _i < _size; _i++)); do
if (( _b[_i] == 1 )); then
_stack+=("${_a[_i]}")
elif (( ${#_stack[@]} > 0 )); then
while (( ${#_stack[@]} > 0 )); do
((_dead++))
local _top=${_stack[-1]}
if (( _a[_i] > _top )); then
unset '_stack[-1]'
_stack=("${_stack[@]}")
else
break
fi
done
fi
done
echo $(( _size - _dead ))
}
This uses a stack for downstream fish and resolves fights only when opposite directions meet.