From 4abaad2051f14cca06b8bff00c460a9c7541f5cb Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Thu, 1 Dec 2022 15:06:30 +0100 Subject: Massively simplify with asort() --- 2022/01/puzzle-2.awk | 18 +++--------------- 1 file 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] } -- cgit v1.2.3