2023-12-02 12:28:49 +01:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
2023-12-10 13:00:08 +01:00
|
|
|
set -o errexit
|
2023-12-02 12:28:49 +01:00
|
|
|
set -o nounset
|
2023-12-10 13:00:08 +01:00
|
|
|
set -o pipefail
|
2023-12-02 12:28:49 +01:00
|
|
|
if [[ "${TRACE-0}" == "1" ]]; then
|
|
|
|
set -o xtrace
|
|
|
|
fi
|
|
|
|
|
2023-12-10 13:00:08 +01:00
|
|
|
INPUT=$1
|
2023-12-02 12:28:49 +01:00
|
|
|
|
|
|
|
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
|
2023-12-10 13:00:08 +01:00
|
|
|
count=$(grep -o "[0-9]* ${color}" <<< "${sets[$i]}" | grep -o '[0-9]*' || true)
|
2023-12-02 12:28:49 +01:00
|
|
|
if [[ $count -gt ${limits[$color]} ]]; then
|
|
|
|
possible=false
|
2023-12-10 13:00:08 +01:00
|
|
|
break 2
|
2023-12-02 12:28:49 +01:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
done
|
|
|
|
if $possible; then
|
|
|
|
id=$(grep -o '[0-9]*' <<< "${game}" | head -n 1)
|
|
|
|
possible_ids+=($id)
|
|
|
|
fi
|
2023-12-10 13:00:08 +01:00
|
|
|
done < "${INPUT}"
|
2023-12-02 12:28:49 +01:00
|
|
|
declare -i sum=0
|
|
|
|
for id in "${possible_ids[@]}"; do
|
|
|
|
(( sum+=id ))
|
|
|
|
done
|
|
|
|
echo $sum
|
|
|
|
}
|
|
|
|
|
|
|
|
main
|