#!/usr/bin/env bash unset PATH readonly PATH source ./utils/main.sh source ./utils/array.sh p1() { local input="$1" a b tmp i local left=() right=() res=() while read -r a b; do left+=("$a") right+=("$b") done <<<"$input" array.timsort left array.timsort right for ((i = 0; i < ${#left[@]}; i++)); do tmp="$((left[i] - right[i]))" res+=("${tmp#-}") done array.sum res } p2() { local input="$1" a b i j local left=() right=() res=() while read -r a b; do left+=("$a") right+=("$b") done <<<"$input" for i in "${left[@]}"; do local tmp=0 for j in "${right[@]}"; do ((i == j)) && ((tmp++)) done res+=("$((i * tmp))") done array.sum res } main "$@"