aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2022-12-01 15:06:30 +0100
committerThomas Voss <mail@thomasvoss.com> 2022-12-01 15:06:30 +0100
commit4abaad2051f14cca06b8bff00c460a9c7541f5cb (patch)
treec0077b3efe47ff0c0a0934771ce9e721cd1bf3fa
parentab1a834e1dded68d7d722148000cce2503b56d57 (diff)
Massively simplify with asort()
-rwxr-xr-x2022/01/puzzle-2.awk18
1 files changed, 3 insertions, 15 deletions
diff --git a/2022/01/puzzle-2.awk b/2022/01/puzzle-2.awk
index 6186084..11f46c0 100755
--- a/2022/01/puzzle-2.awk
+++ b/2022/01/puzzle-2.awk
@@ -1,17 +1,5 @@
-#!/usr/bin/awk -f
+#!/usr/bin/gawk -f
/[0-9]+/ { acc += $1 }
-!/./ {
- if (acc > max[1]) {
- max[3] = max[2];
- max[2] = max[1];
- max[1] = acc;
- } else if (acc > max[2]) {
- max[3] = max[2];
- max[2] = acc;
- } else if (acc > max[3])
- max[3] = acc;
- acc = 0
-}
-
-END { print max[1] + max[2] + max[3] }
+!/./ { max[++i] = acc; acc = 0 }
+END { asort(max); print max[i] + max[i - 1] + max[i - 2] }