This commit is contained in:
2021-12-10 10:55:53 +01:00
parent 000a72bef5
commit f2ef26fa6e
2 changed files with 104 additions and 0 deletions

62
10/02.sh Executable file
View File

@@ -0,0 +1,62 @@
#!/bin/bash
scores=()
while read line; do
chars=( $(sed 's/\(.\)/\1 /g' <<< $line) )
opened_chars=()
broken=0
for char in ${chars[@]}; do
case $char in
'('|'['|'{'|'<')
opened_chars+=( $char );;
')')
if [ ${opened_chars[-1]} = '(' ]; then
unset 'opened_chars[-1]'
else
broken=1
break
fi;;
']')
if [ ${opened_chars[-1]} = '[' ]; then
unset 'opened_chars[-1]'
else
broken=1
break
fi;;
'}')
if [ ${opened_chars[-1]} = '{' ]; then
unset 'opened_chars[-1]'
else
broken=1
break
fi;;
'>')
if [ ${opened_chars[-1]} = '<' ]; then
unset 'opened_chars[-1]'
else
broken=1
break
fi;;
esac
done
if [ $broken -eq 0 ]; then
score=0
n_opened_chars=${#opened_chars[@]}
for i in $(seq $((n_opened_chars-1)) -1 0); do
case ${opened_chars[$i]} in
'(')
score=$((score*5+1));;
'[')
score=$((score*5+2));;
'{')
score=$((score*5+3));;
'<')
score=$((score*5+4));;
esac
done
scores+=( $score )
fi
done
scores=( $(printf '%s\n' "${scores[@]}" | sort -nr) )
n_scores=${#scores[@]}
echo ${scores[$((n_scores/2))]}