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