From a0c0f280f07a965e1fa4ffb8497715850d34e399 Mon Sep 17 00:00:00 2001 From: Annwan Date: Wed, 6 Dec 2023 12:56:38 +0100 Subject: [PATCH] Day 4, awk --- day4/1.awk | 30 ++++++++++++++++++++++++++++++ day4/2.awk | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 day4/1.awk create mode 100644 day4/2.awk diff --git a/day4/1.awk b/day4/1.awk new file mode 100644 index 0000000..bd3a1bc --- /dev/null +++ b/day4/1.awk @@ -0,0 +1,30 @@ +BEGIN { + sum = 0 +} +{ + cardNo = $2 + delete winingNumbers; + delete myNumbers; + for (i = 3; $i != "|"; i++) { + winingNumbers[i - 2] = $i; + } + for(c = 1;i+c <= NF;c++) { + myNumbers[c] = $(i + c) + } + score = 0; + for (i = 1; i <= length(myNumbers); i++) { + good = 0; + for (j = 1; j <= length(winingNumbers); j++) { + if (myNumbers[i] == winingNumbers[j]) { + good = 1; + break; + } + } + if (!good) continue; + if (score == 0) score = 1; else score = score * 2; + } + sum = sum + score; +} +END { + print sum; +} diff --git a/day4/2.awk b/day4/2.awk new file mode 100644 index 0000000..d316a78 --- /dev/null +++ b/day4/2.awk @@ -0,0 +1,37 @@ +BEGIN { + delete cards; + delete counts; +} +{ + cardNo = substr($2, 1, length($2) - 1); + delete winingNumbers; + delete myNumbers; + for (i = 3; $i != "|"; i++) { + winingNumbers[i - 2] = $i; + } + for(c = 1;i+c <= NF;c++) { + myNumbers[c] = $(i + c) + } + wins = 0; + for (i = 1; i <= length(myNumbers); i++) { + for (j = 1; j <= length(winingNumbers); j++) { + if (myNumbers[i] == winingNumbers[j]) { + wins = wins + 1; + break; + } + } + } + cards[cardNo] = wins; + counts[cardNo] = 1; +} +END { + count = 0 + for (i = 1; i <= NR; i++) { + count = count + counts[i]; + for(n = 1; n <= cards[i]; n++) { + counts[i + n] = counts[i + n] + counts[i]; + } + } + print count; +} +