From 92f9c9b1b5dfaf5a34cd5130f1f05b0c619f534f Mon Sep 17 00:00:00 2001 From: Annwan Date: Sat, 2 Dec 2023 16:41:43 +0100 Subject: [PATCH] Day 2, awk --- day2.awk | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 day2.awk diff --git a/day2.awk b/day2.awk new file mode 100644 index 0000000..9d5499a --- /dev/null +++ b/day2.awk @@ -0,0 +1,40 @@ +BEGIN { + FS = ":" + maxred = 12; + maxgreen = 13; + maxblue = 14; +} +{ + gamenum = substr($1, 6) + split($2, games, ";"); + valid = 1; + minred = 0; + minblue = 0; + mingreen = 1; + for (i = 1; i <= length(games); i++) { + split(games[i], game, ","); + usedblue = 0; + usedred = 0; + usedgreen = 0; + + for (j = 1; j <= length(game); j++) { + split(game[j], value, " ") + if (value[2] == "blue") + usedblue = usedblue + value[1]; + else if (value[2] == "red") + usedred = usedred + value[1]; + else if (value[2] == "green") + usedgreen = usedgreen + value[1]; + else + print "Unknown color in `", game[j], "`"; + } + if (usedblue > minblue) minblue = usedblue; + if (usedred > minred) minred = usedred; + if (usedgreen > mingreen) mingreen = usedgreen; + valid = valid && usedred <= maxred && usedblue <= maxblue && usedgreen <= maxgreen; + } + if (valid) + sum1 = sum1 + gamenum; + sum2 = sum2 + (minred * mingreen * minblue); +} +END { print sum1, sum2; }