From c037c070860ab18d2022ff9a86cc3042d2a00242 Mon Sep 17 00:00:00 2001 From: Tom Bloor Date: Thu, 5 Dec 2024 00:56:14 +0000 Subject: [PATCH] first pass with demo input --- day/01/demo_input.txt | 6 ++++ day/01/part1.sh | 64 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 day/01/demo_input.txt create mode 100755 day/01/part1.sh diff --git a/day/01/demo_input.txt b/day/01/demo_input.txt new file mode 100644 index 0000000..b8af9ad --- /dev/null +++ b/day/01/demo_input.txt @@ -0,0 +1,6 @@ +3 4 +4 3 +2 5 +1 3 +3 9 +3 3 diff --git a/day/01/part1.sh b/day/01/part1.sh new file mode 100755 index 0000000..2b755ef --- /dev/null +++ b/day/01/part1.sh @@ -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}"