diff options
author | Thomas Voss <mail@thomasvoss.com> | 2022-12-02 15:27:20 +0100 |
---|---|---|
committer | Thomas Voss <mail@thomasvoss.com> | 2022-12-02 15:56:44 +0100 |
commit | 231b40909b5a3c2e202bcb48ab40e42a4213bdff (patch) | |
tree | 41a96c3c7fdb650a2cd9e68e05f8568499612f4f | |
parent | 151316492aa68d0e76e462d21c53a903dcdbe601 (diff) |
Add another gawk solution
-rw-r--r-- | 2022/01/README | 5 | ||||
-rwxr-xr-x | 2022/01/puzzles.awk | 12 |
2 files changed, 17 insertions, 0 deletions
diff --git a/2022/01/README b/2022/01/README new file mode 100644 index 0000000..e991bf0 --- /dev/null +++ b/2022/01/README @@ -0,0 +1,5 @@ +puzzle-1.awk and puzzle-2.awk were my original solutions. puzzles.awk is a +solution I wrote afterwards while reading the Gawk documentation, because I +wanted to play around with setting RS to the empty string (this allows you to +have blank-line seperated records). It also works, and works just fine, so I'll +leave it in this repo. diff --git a/2022/01/puzzles.awk b/2022/01/puzzles.awk new file mode 100755 index 0000000..11bece5 --- /dev/null +++ b/2022/01/puzzles.awk @@ -0,0 +1,12 @@ +#!/usr/bin/gawk -f + +BEGIN { RS = ""; FS = "\n" } + { for (i = 1; i <= NF; i++) s[NR] += $i } +END { + asort(s) + # START PART 1 + print s[NR] + # END PART 1 START PART 2 + print s[NR] + s[NR - 1] + s[NR - 2] + # END PART 2 +} |