From: Brett Parker Date: Fri, 4 Dec 2020 13:37:16 +0000 (+0000) Subject: Add bash versions of day 1 for shits and giggles X-Git-Url: https://git.sommitrealweird.co.uk/advent-of-code-2020.git/commitdiff_plain/ee13040bdd49fa89d85bcd516fe6f849f868a22e Add bash versions of day 1 for shits and giggles --- diff --git a/day1/find_2020_factors.sh b/day1/find_2020_factors.sh new file mode 100644 index 0000000..fa3a5d5 --- /dev/null +++ b/day1/find_2020_factors.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +# read the file straight in for gits and shiggles +declare -a data +read -a data -d $'\0' < input.txt + +echo "Got ${#data[*]} records" + +for a in $(seq 0 $((${#data[*]}-2))); do + for b in $(seq $((a+1)) $((${#data[*]}-1))); do + if [ $((${data[$a]} + ${data[$b]})) -eq 2020 ]; then + echo "Match found: ${data[$a]} + ${data[$b]} = 2020" + echo "Multiplied: $((${data[$a]} * ${data[$b]}))" + exit 0 + fi + done +done diff --git a/day1/find_2020_factors_2.sh b/day1/find_2020_factors_2.sh new file mode 100644 index 0000000..04af85d --- /dev/null +++ b/day1/find_2020_factors_2.sh @@ -0,0 +1,22 @@ +#!/bin/bash + +# read the file straight in for gits and shiggles +declare -a data +read -a data -d $'\0' < input.txt + +echo "Got ${#data[*]} records" + +for a in $(seq 0 $((${#data[*]}-3))); do + progress=$(($((a * 100)) / ${#data[*]})) + echo -ne "\r${progress}% through" + for b in $(seq $((a+1)) $((${#data[*]}-2))); do + for c in $(seq $((a+2)) $((${#data[*]}-1))); do + if [ $((${data[$a]} + ${data[$b]} + ${data[$c]})) -eq 2020 ]; then + echo -e "\r100% done " + echo "Match found: ${data[$a]} + ${data[$b]} + ${data[$c]} = 2020" + echo "Multiplied: $((${data[$a]} * ${data[$b]} * ${data[$c]}))" + exit 0 + fi + done + done +done