|
2 | 2 |
|
3 | 3 | test -z "$1" && echo "usage: $0 <path-to-input>" && exit 1
|
4 | 4 |
|
5 |
| -nl=`awk 'END { print NR }' "$1"` |
6 |
| -echo "total cards $nl" |
| 5 | +nc=`awk 'END { print NR }' "$1"` |
7 | 6 |
|
8 |
| -cat "$1" | tr -d '|' | nl | sort -nr | cut -f2- | awk -vnl=$nl ' |
| 7 | +cat "$1" | tr -d '|' | nl | sort -nr | cut -f2- | awk -vnc=$nc ' |
9 | 8 | {
|
10 |
| - p=0; s=0; matches=0; points=0; |
11 |
| - for(i=3;i<=NF;i++) t[$i]+=1 |
12 |
| - for(k in t) if(t[k]>1) {p+=1; matches+=1;} |
13 |
| - if(p>0) s+=2^(p-1) |
14 |
| - ss += s; |
15 |
| - delete t |
16 |
| -
|
17 |
| - if (matches>0) { points=2^(matches-1)}; |
18 |
| -
|
19 |
| -
|
20 |
| - cardnum=nl-NR+1 |
21 |
| - instances[cardnum]=1; |
22 |
| - g_matches[cardnum]=matches; |
23 |
| - wins[cardnum]=0; |
24 |
| - print "card", cardnum, "matches", g_matches[cardnum], "points", points |
25 |
| -
|
26 |
| - for(i=1; i<=matches; i++) { |
27 |
| - j = cardnum+i |
28 |
| - print "> we win one extra instance of card", j, "which itself had", wins[j], "wins" |
29 |
| - wins[cardnum]+=(wins[j]+1); |
30 |
| - } |
31 |
| - print "card", cardnum, "wins", wins[cardnum] |
32 |
| -
|
| 9 | + matches = 0 |
| 10 | + for (i = 3; i <= NF; i++) count[$i] += 1 |
| 11 | + for (k in count) if (count[k] > 1) matches += 1 |
| 12 | + delete count |
| 13 | + points = 0 |
| 14 | + if (matches > 0) points = 2 ^ (matches-1) |
| 15 | + score += points |
| 16 | +
|
| 17 | + i = nc - NR + 1 |
| 18 | + for (j = 1; j <= matches; j++) wins[i] += (wins[i+j] + 1) |
33 | 19 | }
|
34 | 20 |
|
35 | 21 | END {
|
36 |
| - print "---" |
37 |
| - totalCards=0; |
38 |
| - for(i=1;i<=nl;i++) { |
39 |
| - print "card", i, "extra wins", wins[i] |
40 |
| - totalCards+=1; # the card itself |
41 |
| - totalCards+=wins[i]; # and the extra cards it caused us to win |
| 22 | + for (i = 1; i <= nc; i++) { |
| 23 | + totalCards += (wins[i] + 1); |
42 | 24 | }
|
43 |
| - print "total scratchcards we are left with is", totalCards |
| 25 | + print score "," totalCards |
44 | 26 | }
|
45 | 27 | '
|
0 commit comments