Skip to content

Instantly share code, notes, and snippets.

@oguz-ismail
Last active November 20, 2023 05:31
Show Gist options
  • Save oguz-ismail/b812d79e783fad9f600864701651a3fa to your computer and use it in GitHub Desktop.
Save oguz-ismail/b812d79e783fad9f600864701651a3fa to your computer and use it in GitHub Desktop.
{
pos[NR] = $NF
}
END {
count_wins(pos[1], pos[2])
if (wins[1] > wins[2])
print wins[1]
else
print wins[2]
}
function count_wins(pos1, pos2,
rolls1, rolls2,
sum1, sum2,
score1, score2, args, old1, old2, i) {
if (rolls1 == 3 && rolls2 == 0) {
pos1 += sum1
if (pos1 > 10)
pos1 -= 10
score1 += pos1
}
else if (rolls2 == 3) {
pos2 += sum2
if (pos2 > 10)
pos2 -= 10
score2 += pos2
rolls1 = sum1 = 0
rolls2 = sum2 = 0
}
if (score1 >= 21) {
wins[1]++
return
}
if (score2 >= 21) {
wins[2]++
return
}
args = pos1 "," pos2 "," \
rolls1 "," rolls2 "," \
sum1 "," sum2 "," \
score1 "," score2
if ((args, 1) in cache) {
wins[1] += cache[args, 1]
wins[2] += cache[args, 2]
return
}
old1 = wins[1]
old2 = wins[2]
if (rolls1 < 3)
for (i = 1; i <= 3; i++)
count_wins(pos1, pos2,
rolls1+1, rolls2,
sum1+i, sum2,
score1, score2)
else
for (i = 1; i <= 3; i++)
count_wins(pos1, pos2,
rolls1, rolls2+1,
sum1, sum2+i,
score1, score2)
cache[args, 1] = wins[1]-old1
cache[args, 2] = wins[2]-old2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment