adventofcode2022/day01/sol.sh

9 lines
274 B
Bash

# solve day 1 with awk and sort
# part 1
awk 'BEGIN{RS="\n\n";m=0} {t=0; for(i=1;i<=NF;i++)t+=$i; if(m<t)m=t} END{print m}' < input
# part 2
awk 'BEGIN{RS="\n\n"} {t=0; for(i=1;i<=NF;i++)t+=$i; print t}' < input | sort -nr |
awk 'BEGIN{t=0} {t+=$1} NR==3 {print t; quit}'