01 AWK

The input is a list of numbers where groups are separated by an empty line. Stored in aoc_01.txt.

This time my hammer is awk.

awk 'BEGIN {
      m=0
      a=0
    }
    /[0-9]*/ {
        a+=$0
    }
    /^\s*$/ {
      if (a>m) {
        m = a
      }
      a=0
    }
    END {
      print m
    }' aoc_01.txt

01B

awk.cmd:

BEGIN {
  a=0
}

/[0-9]*/ {
  a+=$0
}

/^\s*$/ {
  print a
  a=0
}

The whole command:

awk -f awk.cmd aoc_01.txt |\
sort -rn |\
head -n3 |\
awk '{sum+=$0} END {print sum}'

What I learned

published: 2022-12-01
last modified: 2023-01-21

https://vit.baisa.cz/notes/code/advent-of-code-2022/01/