factor out sum function

This commit is contained in:
Patrick Michl 2022-12-01 19:11:19 +01:00
parent b6a36aabd3
commit e2cfb6254d
No known key found for this signature in database
GPG Key ID: BFE0ACEE21CD5EB0
2 changed files with 10 additions and 3 deletions

View File

@ -23,9 +23,7 @@ p1() {
p2() { p2() {
read -ra cals <<< "$(cals_per_elf "$1")" read -ra cals <<< "$(cals_per_elf "$1")"
local result=0 utils.sum "${cals[@]: -3}"
for i in "${cals[@]: -3}"; do ((result+=i)); done
echo "$result"
} }
p1 "$INPUT" p1 "$INPUT"

View File

@ -15,6 +15,15 @@ utils.qsort() {
read -ra smaller_sorted <<< "$(utils.qsort "${smaller[@]}")" read -ra smaller_sorted <<< "$(utils.qsort "${smaller[@]}")"
read -ra bigger_sorted <<< "$(utils.qsort "${bigger[@]}")" read -ra bigger_sorted <<< "$(utils.qsort "${bigger[@]}")"
res=("${smaller_sorted[@]}" "$pivot" "${bigger_sorted[@]}") res=("${smaller_sorted[@]}" "$pivot" "${bigger_sorted[@]}")
echo "${res[@]}" echo "${res[@]}"
} }
utils.sum() {
read -ra input <<< "$@"
local result=0
for i in "${input[@]}"; do ((result+=i)); done
echo "$result"
}