Skip to content

Commit 570ad95

Browse files
committed
Cleanup
1 parent 4a3d365 commit 570ad95

File tree

1 file changed

+15
-33
lines changed

1 file changed

+15
-33
lines changed

04.az.sh

+15-33
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,26 @@
22

33
test -z "$1" && echo "usage: $0 <path-to-input>" && exit 1
44

5-
nl=`awk 'END { print NR }' "$1"`
6-
echo "total cards $nl"
5+
nc=`awk 'END { print NR }' "$1"`
76

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 '
98
{
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)
3319
}
3420
3521
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);
4224
}
43-
print "total scratchcards we are left with is", totalCards
25+
print score "," totalCards
4426
}
4527
'

0 commit comments

Comments
 (0)