My solutions to the advent of code
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.
 
 
 

108 lines
2.2 KiB

#!/usr/bin/env bash
file="$1"
gamma=""
epsilon=""
countPositionBits(){
ones=(0 0 0 0 0 0 0 0 0 0 0 0)
#ones=(0 0 0 0 0)
stdin="$(cat -)"
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 .)
done < <(echo "$stdin")
printf '%s\n' "${ones[@]}"
}
dots(){
seq $i | sed 's/.*/./' | tr -d '\n'
}
ogr="$(cat "$1")"
csr="$(cat "$1")"
echo "OGR"
i=0
while [ "$(echo "$ogr" | wc -l)" -gt 1 ]; do
counts="$(echo "$ogr" | countPositionBits)"
noofogr="$(echo "$ogr" | wc -l)"
noofones="$( echo "$counts" | sed -n "$((i + 1))p" )"
echo "$ogr"
echo "No of ones $noofones"
echo "No of ogr $noofogr"
if [ "$((noofones * 10))" -ge "$(( noofogr * 10 / 2 ))" ]; then
echo "Filtering only 1s in position $i"
echo "^$(dots $i)1"
ogr="$(echo "$ogr" | grep "^$(dots $i)1" )"
else
echo "Filtering only 0s in position $i"
ogr="$(echo "$ogr" | grep "^$(dots $i)0" )"
fi
echo -e "\n---\n"
i="$((i + 1))"
done
echo "CSR"
i=0
while [ "$(echo "$csr" | wc -l)" -gt 1 ]; do
counts="$(echo "$csr" | countPositionBits)"
noofcsr="$(echo "$csr" | wc -l)"
noofones="$( echo "$counts" | sed -n "$((i + 1))p" )"
echo "$csr"
echo "No of ones $noofones"
echo "No of csr $noofcsr"
if [ "$((noofones * 10))" -ge "$(( noofcsr * 10 / 2 ))" ]; then
echo "Filtering only 0s in position $i"
echo "^$(dots $i)1"
csr="$(echo "$csr" | grep "^$(dots $i)0" )"
else
echo "Filtering only 1s in position $i"
csr="$(echo "$csr" | grep "^$(dots $i)1" )"
fi
echo -e "\n---\n"
i="$((i + 1))"
done
ogrDecimal="$( echo "ibase=2; $ogr" | bc )"
csrDecimal="$( echo "ibase=2; $csr" | bc )"
echo "Oxxygen Generator Rating: $ogr -> $ogrDecimal"
echo "CO2 Scrubber Rating: $csr -> $csrDecimal"
echo "Answer: $((ogrDecimal * csrDecimal))"
#while read i; do
# if [ "$i" -gt "$(( lines / 2 ))" ]; then
# gamma="${gamma}1"
# epsilon="${epsilon}0"
# else
# gamma="${gamma}0"
# epsilon="${epsilon}1"
# fi
#done < <(echo "")
#
#gammaDecimal="$( echo "ibase=2; $gamma" | bc )"
#epsilonDecimal="$( echo "ibase=2; $epsilon" | bc )"
#
#echo "Gamma: $gamma -> $gammaDecimal"
#echo "Epsilon: $epsilon -> $epsilonDecimal"
#
#echo "Answer: $((gammaDecimal * epsilonDecimal))"