Skip to content

Instantly share code, notes, and snippets.

@tom-henderson
Created December 2, 2022 02:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tom-henderson/ab3151fd03c9ba1417000199ababad6d to your computer and use it in GitHub Desktop.
Save tom-henderson/ab3151fd03c9ba1417000199ababad6d to your computer and use it in GitHub Desktop.
#!/bin/bash
totals=()
while read -r item; do
cal=$((cal + item))
if [ -z "$item" ]; then
totals+=($cal)
cal=0
fi
done < input
totals+=($cal)
IFS=$'\n'
echo "Max is $(echo "${totals[*]}" | sort -nr | head -n1)"
total=0
while read -r n; do
total=$((total + n))
done < <(echo "${totals[*]}" | sort -nr | head -n3)
echo "Total for top 3 is $total"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment