diff --git a/day/01/part2.sh b/day/01/part2.sh new file mode 100755 index 0000000..c9467d6 --- /dev/null +++ b/day/01/part2.sh @@ -0,0 +1,47 @@ +#! /bin/bash + +#set -x + +FILE_IN="${1}" + +left_list=() +right_list=() + +while read -r l; do + echo "reading new line [$l]" + IFS=" " + read -ra line_split <<< "${l}" + unset IFS + echo "first number [${line_split[0]}]" + echo "second number [${line_split[1]}]" + left_list+=("${line_split[0]}") + right_list+=("${line_split[1]}") +done <"$FILE_IN" + +echo "${left_list[@]}" +echo "${right_list[@]}" + +if [[ ${#left_list[@]} -ne ${#right_list[@]} ]]; then + echo "arrays dont match, wtf?" + exit 1 +else + echo "length of lists ${#left_list[@]}" +fi + +list_similarity=() + +for i in "${left_list[@]}"; do + echo "checking for ${i}" + IFS=$'\n' + count="$(grep -oc "${i}" <<<"${right_list[*]}")" + unset IFS + list_similarity+=($((i*count))) +done + +final_out=0 + +for v in "${list_similarity[@]}"; do + final_out=$((final_out + v)) +done + +echo "final output: ${final_out}"