#!/usr/bin/env bash set -o errexit set -o nounset set -o pipefail if [[ "${TRACE-0}" == "1" ]]; then set -o xtrace fi cd "$(dirname "$0")" main () { declare -i sum=0 while read -r line; do declare -a winning=() mine=() mywinning=() declare -i card_sum=0 echo "$line" stripped=$(sed 's/Card\s\{1,3\}[0-9]\{1,3\}: //' <<< "${line}") winning+=($(cut -d '|' -f 1 <<< "${stripped}")) mine+=($(cut -d '|' -f 2 <<< "${stripped}")) for number in "${mine[@]}"; do for w in "${winning[@]}"; do if [[ ${number} -eq ${w} ]]; then mywinning+=("${number}") break fi done done echo mywwinning ${mywinning[@]} for number in "${mywinning[@]}"; do if [[ card_sum -eq 0 ]]; then card_sum=$((card_sum+1)) else card_sum=$((card_sum*2)) fi done echo "card sum: $card_sum" sum=$((sum+card_sum)) echo "sum: ${sum}" done < input.txt echo "${sum}" } main "$@"