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; }