Browse Source

Day 4, awk

main
Annwan 1 year ago
parent
commit
a0c0f280f0
  1. 30
      day4/1.awk
  2. 37
      day4/2.awk

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

37
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;
}
Loading…
Cancel
Save