adventofcode2022/day01/sol.awk

19 lines
331 B
Awk
Raw Normal View History

2022-12-02 02:20:17 +00:00
# Pure awk solution for day 1
# run me with: awk -f sol.awk <input
function insert(v) {
for(i=1;i<=3&&a[i]<v;i++) {
a[i-1]=a[i];
a[i]=v;
}
}
BEGIN { RS="\n\n" }
{ t=0; for(i=1;i<=NF;i++) t+=$i; insert(t) }
END { print a[3] } # part 1
END { t=0; for(i=1;i<=3;i++) t+=a[i]; print t } # part 2