first pass with demo input
This commit is contained in:
parent
e5ddc08a5a
commit
c037c07086
2 changed files with 70 additions and 0 deletions
6
day/01/demo_input.txt
Normal file
6
day/01/demo_input.txt
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
3 4
|
||||
4 3
|
||||
2 5
|
||||
1 3
|
||||
3 9
|
||||
3 3
|
||||
64
day/01/part1.sh
Executable file
64
day/01/part1.sh
Executable file
|
|
@ -0,0 +1,64 @@
|
|||
#! /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
|
||||
|
||||
echo "sorting time"
|
||||
|
||||
IFS=$'\n'
|
||||
mapfile sorted_left_list <<<"$(sort <<<"${left_list[*]}")"
|
||||
mapfile sorted_right_list <<<"$(sort <<<"${right_list[*]}")"
|
||||
unset IFS
|
||||
|
||||
echo "${sorted_left_list[@]}"
|
||||
echo "${sorted_right_list[@]}"
|
||||
|
||||
sorted_list_diff=()
|
||||
|
||||
echo "adding time"
|
||||
|
||||
for i in "${!sorted_left_list[@]}"; do
|
||||
echo "left index: ${i} value: ${sorted_left_list[$i]}"
|
||||
echo "right index: ${i} value: ${sorted_right_list[$i]}"
|
||||
|
||||
i_diff=$((${sorted_left_list[$i]}-${sorted_right_list[$i]}))
|
||||
|
||||
echo "diff ${i_diff}"
|
||||
|
||||
sorted_list_diff+=("${i_diff#-}")
|
||||
done
|
||||
|
||||
echo "${sorted_list_diff[@]}"
|
||||
|
||||
final_out=0
|
||||
|
||||
for v in "${sorted_list_diff[@]}"; do
|
||||
final_out=$((final_out + v))
|
||||
done
|
||||
|
||||
echo "final output: ${final_out}"
|
||||
Loading…
Add table
Reference in a new issue