This commit is contained in:
ida schmidt 2023-12-08 19:56:32 -07:00
parent 6bce41083a
commit 06cf946734

18
day4.sh
View file

@ -2,21 +2,21 @@
#set -vx
declare -a game
while read card; do
echo $card
c="$(echo "${card%\:*}" | tr -cd [:digit:])"
game[$c]=${game[$c]:-0}
echo $card # show what's being processed
c="$(echo "${card%\:*}" | tr -cd [:digit:])" # grab the game number
game[$c]=${game[$c]:-0} # set wins to zero if game isnt initialized
while read win; do
while read num; do
if [[ $num == $win ]] && [[ $num != "" ]];
if [[ $num == $win ]] && [[ $num != "" ]]; # check if number matches a winning number
then
(( game[$c]++ ))
(( game[$c]++ )) # increment win count for game
fi
done < <( echo -n "${card#*\:}" | cut -d '|' -f 2 | tr ' ' '\n' | sed '/^$/d' )
done < <( echo -n "${card#*\:}" | cut -d '|' -f 2 | tr ' ' '\n' | sed '/^$/d' ) # seperate scractcher numbers (delimiting by newline)
done < <( echo -n "${card#*\:}" | cut -d '|' -f 1 | tr ' ' '\n' | sed '/^$/d' )
done < <( echo -n "${card#*\:}" | cut -d '|' -f 1 | tr ' ' '\n' | sed '/^$/d' ) # seperate winning numbers
done< <( cat $1 )
done< <( cat $1 ) # file input
for wins in ${game[@]}; do
echo $(( 2**($wins-1) ))
echo $(( 2**($wins-1) )) # calculate score for all games
done | tr '\n' '+' | sed 's/+$/\n/' | bc