add part 2 script
This commit is contained in:
parent
2a8db803c0
commit
d86111e630
1 changed files with 47 additions and 0 deletions
47
day/01/part2.sh
Executable file
47
day/01/part2.sh
Executable 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}"
|
||||
Loading…
Add table
Reference in a new issue