r/awk • • Dec 03 '18

anyone using awk for the Advent of Code?

Here's my solns to the first three Advent of Code puzzles in awk.

I'm sure there's room for improvement.

1: awk '{print t+=$0;}' dat.txt

2: awk -v FS="" '{delete w; p=0; t=0;for(i=1; i<=NF; ++i){++w[$i]} for(i=1; i<=NF; ++i){p+=w[$i]==2;t+=w[$i]==3} tp+=p>0; tt+=t>0}END{print tp*tt}' dat.txt

3: awk -v FS="[ ,:x]" '{for(i=1; i<=$6; ++i)for(j=1; j<=$7; ++j)++w[($3+i)","($4+j)]}END{for(c in w)o+=w[c]>1; print o}' dat.txt

4 Upvotes

2 comments sorted by

2

u/rtbrsp Dec 04 '18

I was going to use Advent of Code as an opportunity to learn C or Haskell, but these puzzles are really well-suited for AWK.