add part 2 script

This commit is contained in:
Tom Bloor 2024-12-05 01:05:29 +00:00
parent 2a8db803c0
commit d86111e630
No known key found for this signature in database
GPG key ID: 8775E856E2754827

47
day/01/part2.sh Executable file
View file

@ -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}"