aoc/4/1/solution.sh

42 lines
879 B
Bash
Executable File

#!/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
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
for number in "${mywinning[@]}"; do
if [[ card_sum -eq 0 ]]; then
card_sum=$((card_sum+1))
else
card_sum=$((card_sum*2))
fi
done
sum=$((sum+card_sum))
done < input.txt
echo "${sum}"
}
main "$@"