You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
745 B
44 lines
745 B
3 years ago
|
#!/usr/bin/env bash
|
||
|
|
||
|
file="$1"
|
||
|
|
||
|
ones=(0 0 0 0 0 0 0 0 0 0 0 0)
|
||
|
lines=0
|
||
|
gamma=""
|
||
|
epsilon=""
|
||
|
|
||
|
while read line; do
|
||
|
|
||
|
bitn=0
|
||
|
while read bit; do
|
||
|
if [ "$bit" -eq 1 ]; then
|
||
|
ones[$bitn]=$((${ones[$bitn]} + 1))
|
||
|
fi
|
||
|
bitn=$(( bitn + 1 ))
|
||
|
done < <(echo "$line" | grep -o .)
|
||
|
|
||
|
lines="$((lines + 1))"
|
||
|
|
||
|
done < <(cat "$file")
|
||
|
|
||
|
echo "${ones[@]}"
|
||
|
|
||
|
for i in ${ones[@]}; do
|
||
|
if [ "$i" -gt "$(( lines / 2 ))" ]; then
|
||
|
gamma="${gamma}1"
|
||
|
epsilon="${epsilon}0"
|
||
|
else
|
||
|
gamma="${gamma}0"
|
||
|
epsilon="${epsilon}1"
|
||
|
fi
|
||
|
done
|
||
|
|
||
|
|
||
|
gammaDecimal="$( echo "ibase=2; $gamma" | bc )"
|
||
|
epsilonDecimal="$( echo "ibase=2; $epsilon" | bc )"
|
||
|
|
||
|
echo "Gamma: $gamma -> $gammaDecimal"
|
||
|
echo "Epsilon: $epsilon -> $epsilonDecimal"
|
||
|
|
||
|
echo "Answer: $((gammaDecimal * epsilonDecimal))"
|