aoc/2/1/solution.sh

42 lines
925 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 -a colors=(red green blue)
declare -A limits=([red]=12 [green]=13 [blue]=14)
declare -a possible_ids
while read -r game; do
possible=true
stripped_game=$(sed 's/^Game [0-9]\+: //' <<< "${game}")
IFS=';' read -r -a sets <<< "${stripped_game}"
for (( i=0; i<${#sets[@]}; i++ )); do
for color in "${colors[@]}"; do
count=$(grep -o "[0-9]* ${color}" <<< "${sets[$i]}" | grep -o '[0-9]*')
if [[ $count -gt ${limits[$color]} ]]; then
possible=false
fi
done
done
if $possible; then
id=$(grep -o '[0-9]*' <<< "${game}" | head -n 1)
possible_ids+=($id)
fi
done < input.txt
declare -i sum=0
for id in "${possible_ids[@]}"; do
(( sum+=id ))
done
echo $sum
}
main