aboutsummaryrefslogtreecommitdiff
path: root/2022/01/puzzle-2.awk
diff options
context:
space:
mode:
Diffstat (limited to '2022/01/puzzle-2.awk')
-rwxr-xr-x2022/01/puzzle-2.awk17
1 files changed, 17 insertions, 0 deletions
diff --git a/2022/01/puzzle-2.awk b/2022/01/puzzle-2.awk
new file mode 100755
index 0000000..d8bbaaa
--- /dev/null
+++ b/2022/01/puzzle-2.awk
@@ -0,0 +1,17 @@
+#!/usr/bin/awk -f
+
+/[0-9]+/ { acc += $1; next }
+{
+ 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] }