From c1875f1bee7c07227942a831e466f2c262339439 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Sat, 2 Dec 2023 15:48:50 +0100 Subject: Add 2023 day 2 solutions --- 2023/02/puzzle-2.awk | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100755 2023/02/puzzle-2.awk (limited to '2023/02/puzzle-2.awk') diff --git a/2023/02/puzzle-2.awk b/2023/02/puzzle-2.awk new file mode 100755 index 0000000..8c19409 --- /dev/null +++ b/2023/02/puzzle-2.awk @@ -0,0 +1,31 @@ +#!/usr/bin/awk -f + +BEGIN { + FS = "[;:] " +} + +{ + r = 0; g = 0; b = 0 + + for (i = 2; i <= NF; i++) { + split($i, xs, /, /) + for (j in xs) { + split(xs[j], ys, / /) + n = ys[1] + c = ys[2] + + if (c == "red" && n > r) + r = n + else if (c == "green" && n > g) + g = n + else if (c == "blue" && n > b) + b = n + } + } + + sum += r * g * b +} + +END { + print sum +} -- cgit v1.2.3