Advent-Of-Code-2024/day/01/part2.sh
2024-12-05 01:05:29 +00:00

47 lines
852 B
Bash
Executable file

#! /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}"